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
5 changed files
with
353 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?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 Closure; | ||
use Generator; | ||
use loophp\iterators\ClosureIteratorAggregate; | ||
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 Traversable; | ||
|
||
/** | ||
* @Groups({"internal"}) | ||
* @Iterations(10) | ||
* @Warmup(5) | ||
* @Revs(100) | ||
*/ | ||
final class ClosureIteratorAggregateBench | ||
{ | ||
/** | ||
* @ParamProviders("provideGenerators") | ||
*/ | ||
public function benchIterator(array $params): void | ||
{ | ||
$callable = Closure::fromCallable([ClosureIteratorAggregateBench::class, 'loop']); | ||
|
||
$this->test( | ||
new $params['class']($callable, [$this->getGenerator($params)]), | ||
$params['size'] | ||
); | ||
} | ||
|
||
public function provideGenerators(): Generator | ||
{ | ||
$items = 5000; | ||
|
||
yield ClosureIteratorAggregate::class => [ | ||
'class' => ClosureIteratorAggregate::class, | ||
'size' => $items, | ||
]; | ||
} | ||
|
||
private function getGenerator(array $params): Generator | ||
{ | ||
for ($i = 0; $i < $params['size']; ++$i) { | ||
yield [$i, sprintf('*%s*', $i)]; | ||
} | ||
} | ||
|
||
private function loop(Traversable $input): Generator | ||
{ | ||
foreach ($input as $key => $value) { | ||
yield [$key, $value]; | ||
} | ||
} | ||
|
||
private function test(ClosureIteratorAggregate $input): void | ||
{ | ||
iterator_to_array($this->loop($input)); | ||
} | ||
} |
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,70 @@ | ||
<?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 Closure; | ||
use Generator; | ||
use loophp\iterators\ClosureIterator; | ||
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 Traversable; | ||
|
||
/** | ||
* @Groups({"internal"}) | ||
* @Iterations(10) | ||
* @Warmup(5) | ||
* @Revs(100) | ||
*/ | ||
final class ClosureIteratorBench | ||
{ | ||
/** | ||
* @ParamProviders("provideGenerators") | ||
*/ | ||
public function benchIterator(array $params): void | ||
{ | ||
$callable = Closure::fromCallable([ClosureIteratorBench::class, 'loop']); | ||
|
||
$this->test( | ||
new $params['class']($callable, [$this->getGenerator($params)]), | ||
$params['size'] | ||
); | ||
} | ||
|
||
public function provideGenerators(): Generator | ||
{ | ||
$items = 5000; | ||
|
||
yield ClosureIterator::class => [ | ||
'class' => ClosureIterator::class, | ||
'size' => $items, | ||
]; | ||
} | ||
|
||
private function getGenerator(array $params): Generator | ||
{ | ||
for ($i = 0; $i < $params['size']; ++$i) { | ||
yield [$i, sprintf('*%s*', $i)]; | ||
} | ||
} | ||
|
||
private function loop(Traversable $input): Generator | ||
{ | ||
foreach ($input as $key => $value) { | ||
yield [$key, $value]; | ||
} | ||
} | ||
|
||
private function test(ClosureIterator $input): void | ||
{ | ||
iterator_to_array($this->loop($input)); | ||
} | ||
} |
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,67 @@ | ||
<?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 Generator; | ||
use loophp\iterators\IterableIteratorAggregate; | ||
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 Traversable; | ||
|
||
/** | ||
* @Groups({"internal"}) | ||
* @Iterations(10) | ||
* @Warmup(5) | ||
* @Revs(100) | ||
*/ | ||
final class IterableIteratorAggregateBench | ||
{ | ||
/** | ||
* @ParamProviders("provideGenerators") | ||
*/ | ||
public function benchIterator(array $params): void | ||
{ | ||
$this->test( | ||
new $params['class']($this->getGenerator($params)), | ||
$params['size'] | ||
); | ||
} | ||
|
||
public function provideGenerators(): Generator | ||
{ | ||
$items = 5000; | ||
|
||
yield IterableIteratorAggregate::class => [ | ||
'class' => IterableIteratorAggregate::class, | ||
'size' => $items, | ||
]; | ||
} | ||
|
||
private function getGenerator(array $params): Generator | ||
{ | ||
for ($i = 0; $i < $params['size']; ++$i) { | ||
yield [$i, sprintf('*%s*', $i)]; | ||
} | ||
} | ||
|
||
private function loop(Traversable $input): Generator | ||
{ | ||
foreach ($input as $key => $value) { | ||
yield [$key, $value]; | ||
} | ||
} | ||
|
||
private function test(IterableIteratorAggregate $input): void | ||
{ | ||
iterator_to_array($this->loop($input)); | ||
} | ||
} |
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,67 @@ | ||
<?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 Generator; | ||
use loophp\iterators\IterableIterator; | ||
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 Traversable; | ||
|
||
/** | ||
* @Groups({"internal"}) | ||
* @Iterations(10) | ||
* @Warmup(5) | ||
* @Revs(100) | ||
*/ | ||
final class IterableIteratorBench | ||
{ | ||
/** | ||
* @ParamProviders("provideGenerators") | ||
*/ | ||
public function benchIterator(array $params): void | ||
{ | ||
$this->test( | ||
new $params['class']($this->getGenerator($params)), | ||
$params['size'] | ||
); | ||
} | ||
|
||
public function provideGenerators(): Generator | ||
{ | ||
$items = 5000; | ||
|
||
yield IterableIterator::class => [ | ||
'class' => IterableIterator::class, | ||
'size' => $items, | ||
]; | ||
} | ||
|
||
private function getGenerator(array $params): Generator | ||
{ | ||
for ($i = 0; $i < $params['size']; ++$i) { | ||
yield [$i, sprintf('*%s*', $i)]; | ||
} | ||
} | ||
|
||
private function loop(Traversable $input): Generator | ||
{ | ||
foreach ($input as $key => $value) { | ||
yield [$key, $value]; | ||
} | ||
} | ||
|
||
private function test(IterableIterator $input): void | ||
{ | ||
iterator_to_array($this->loop($input)); | ||
} | ||
} |
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,79 @@ | ||
<?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 Generator; | ||
use loophp\iterators\PausableIteratorAggregate; | ||
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 Traversable; | ||
|
||
/** | ||
* @Groups({"internal"}) | ||
* @Iterations(10) | ||
* @Warmup(5) | ||
* @Revs(100) | ||
*/ | ||
final class PausableIteratorAggregateBench | ||
{ | ||
/** | ||
* @ParamProviders("provideGenerators") | ||
*/ | ||
public function benchIterator(array $params): void | ||
{ | ||
$this->test( | ||
new $params['class']($this->getGenerator($params)), | ||
$params['size'] | ||
); | ||
} | ||
|
||
public function provideGenerators(): Generator | ||
{ | ||
$items = 5000; | ||
|
||
yield PausableIteratorAggregate::class => [ | ||
'class' => PausableIteratorAggregate::class, | ||
'size' => $items, | ||
]; | ||
} | ||
|
||
private function getGenerator(array $params): Generator | ||
{ | ||
for ($i = 0; $i < $params['size']; ++$i) { | ||
yield [$i, sprintf('*%s*', $i)]; | ||
} | ||
} | ||
|
||
private function loop(Traversable $input): Generator | ||
{ | ||
foreach ($input as $key => $value) { | ||
yield [$key, $value]; | ||
} | ||
} | ||
|
||
private function loopUntil(Traversable $input, int $size): Generator | ||
{ | ||
foreach ($input as $key => $value) { | ||
yield [$key, $value]; | ||
|
||
if (0 === $size--) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
private function test(PausableIteratorAggregate $input, int $size): void | ||
{ | ||
iterator_to_array($this->loopUntil($input, $size / 2)); | ||
iterator_to_array($this->loop($input->rest())); | ||
} | ||
} |