-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
87 additions
and
1 deletion.
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,10 @@ | ||
{ | ||
"$schema":"./vendor/phpbench/phpbench/phpbench.schema.json", | ||
"runner.bootstrap": "vendor/autoload.php", | ||
"runner.path": "tests/benchmarks", | ||
"report.generators": { | ||
"test": { | ||
"extends": "aggregate" | ||
} | ||
} | ||
} |
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,73 @@ | ||
<?php | ||
|
||
/** | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace benchmarks\loophp\iterators; | ||
|
||
use Exception; | ||
use Generator; | ||
use loophp\iterators\CachingIteratorAggregate; | ||
use PhpBench\Benchmark\Metadata\Annotations\Groups; | ||
use PhpBench\Benchmark\Metadata\Annotations\Iterations; | ||
use PhpBench\Benchmark\Metadata\Annotations\Revs; | ||
use PhpBench\Benchmark\Metadata\Annotations\Warmup; | ||
use Psl\Iter\Iterator as IterIterator; | ||
use Traversable; | ||
|
||
/** | ||
* @Groups({"GeneratorCache"}) | ||
* @Iterations(10) | ||
* @Warmup(1) | ||
* @Revs(100) | ||
*/ | ||
class GeneratorCacheBench | ||
{ | ||
/** | ||
* @ParamProviders("provideGenerators") | ||
*/ | ||
public function benchIterators(array $params): void | ||
{ | ||
$generator = static function (): Generator { | ||
yield true => true; | ||
|
||
yield false => false; | ||
|
||
yield ['a'] => ['a']; | ||
|
||
yield from range(0, 250); | ||
}; | ||
|
||
$iterator = new $params['class']($generator()); | ||
|
||
$this->test($iterator); | ||
} | ||
|
||
public function provideGenerators(): Generator | ||
{ | ||
yield 'loophp/iterators' => ['class' => CachingIteratorAggregate::class]; | ||
|
||
yield 'azjezz/psl' => ['class' => IterIterator::class]; | ||
} | ||
|
||
private function test(Traversable $input): void | ||
{ | ||
$a = $b = []; | ||
|
||
foreach ($input as $key => $value) { | ||
$a[] = [$key, $value]; | ||
} | ||
|
||
foreach ($input as $key => $value) { | ||
$b[] = [$key, $value]; | ||
} | ||
|
||
if ($a !== $b) { | ||
throw new Exception('$a !== $b'); | ||
} | ||
} | ||
} |