-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
benchmark against native functions and improve readme
- Loading branch information
1 parent
1146cd3
commit 5169533
Showing
6 changed files
with
251 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
/** | ||
* @BeforeMethods({"init"}) | ||
*/ | ||
class MapBench | ||
{ | ||
public function init() | ||
{ | ||
$this->items = self::getItems(); | ||
} | ||
|
||
private static function getItems() | ||
{ | ||
$numberOfItems = 10000; | ||
$items = []; | ||
for($i = 0; $i < $numberOfItems; $i++) { | ||
$object = new stdClass; | ||
$object->number = $i; | ||
$items[] = $object; | ||
} | ||
return $items; | ||
} | ||
/** | ||
* @Revs(1000) | ||
* @Iterations(5) | ||
*/ | ||
public function benchNative() | ||
{ | ||
$items = $this->items; | ||
$numbers = \array_map(static function($item) { | ||
return $item->number; | ||
}, $items); | ||
} | ||
/** | ||
* @Revs(1000) | ||
* @Iterations(5) | ||
*/ | ||
public function benchLazy() | ||
{ | ||
$items = $this->items; | ||
$numbers = \LazyLists\map(static function($item) { | ||
return $item->number; | ||
}, $items); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
/** | ||
* @BeforeMethods({"init"}) | ||
*/ | ||
class PipeFilterMapReduceBench | ||
{ | ||
public function __construct() | ||
{ | ||
$this->pipe = \LazyLists\pipe( | ||
\LazyLists\filter(static function($item) { | ||
return $item->number % 5 === 0; | ||
}), | ||
\LazyLists\map(static function($item) { | ||
return $item->number; | ||
}), | ||
\LazyLists\reduce(static function($sum, $number) { | ||
return $sum + $number; | ||
}, 0) | ||
); | ||
} | ||
public function init() | ||
{ | ||
$this->items = self::getItems(); | ||
} | ||
|
||
private static function getItems() | ||
{ | ||
$numberOfItems = 10000; | ||
$items = []; | ||
for($i = 0; $i < $numberOfItems; $i++) { | ||
$object = new stdClass; | ||
$object->number = $i; | ||
$items[] = $object; | ||
} | ||
return $items; | ||
} | ||
/** | ||
* @Revs(400) | ||
* @Iterations(5) | ||
*/ | ||
public function benchNative() | ||
{ | ||
$items = $this->items; | ||
$divisibleBy5 = \array_filter($items, static function($item) { | ||
return $item->number % 5 === 0; | ||
}); | ||
$numbers = \array_map(static function($item) { | ||
return $item->number; | ||
}, $divisibleBy5); | ||
$sum = \array_reduce($numbers, static function($sum, $number) { | ||
return $sum + $number; | ||
}, 0); | ||
} | ||
/** | ||
* @Revs(400) | ||
* @Iterations(5) | ||
*/ | ||
public function benchLazy() | ||
{ | ||
$items = $this->items; | ||
$pipe = $this->pipe; | ||
$sum = $pipe($items); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
/** | ||
* @BeforeMethods({"init"}) | ||
*/ | ||
class PipeMapFilterTakeBench | ||
{ | ||
public function __construct() | ||
{ | ||
$this->pipe = \LazyLists\pipe( | ||
\LazyLists\map(static function($item) { | ||
return $item->number; | ||
}), | ||
\LazyLists\filter(static function($number) { | ||
return $number % 5 === 0; | ||
}), | ||
\LazyLists\take(50) | ||
); | ||
} | ||
public function init() | ||
{ | ||
$this->items = self::getItems(); | ||
} | ||
|
||
private static function getItems() | ||
{ | ||
$numberOfItems = 10000; | ||
$items = []; | ||
for($i = 0; $i < $numberOfItems; $i++) { | ||
$object = new stdClass; | ||
$object->number = $i; | ||
$items[] = $object; | ||
} | ||
return $items; | ||
} | ||
/** | ||
* @Revs(400) | ||
* @Iterations(5) | ||
*/ | ||
public function benchNative() | ||
{ | ||
$items = $this->items; | ||
$numbers = \array_map(static function($item) { | ||
return $item->number; | ||
}, $items); | ||
$divisibleBy5 = \array_filter($numbers, static function($number) { | ||
return $number % 5 === 0; | ||
}); | ||
$first50 = \array_slice($divisibleBy5, 0, 50); | ||
} | ||
/** | ||
* @Revs(400) | ||
* @Iterations(5) | ||
*/ | ||
public function benchLazy() | ||
{ | ||
$items = $this->items; | ||
$pipe = $this->pipe; | ||
$first50 = $pipe($items); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"bootstrap": "vendor/autoload.php" | ||
} |