-
-
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.
(cherry picked from commit e42a5af45e90f861ce279bc6cd25511fc65e2a84)
- Loading branch information
Showing
12 changed files
with
133 additions
and
258 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
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
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
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 | ||
|
||
/** | ||
* 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 PhpBench\Benchmark\Metadata\Annotations\ParamProviders; | ||
use Traversable; | ||
|
||
abstract class IteratorBenchmark | ||
{ | ||
/** | ||
* @ParamProviders("provideGenerators") | ||
*/ | ||
abstract public function bench(array $params): void; | ||
|
||
abstract public function provideGenerators(): Generator; | ||
|
||
protected function doBench(Traversable $input, array $params): void | ||
{ | ||
iterator_to_array($this->loop($input)); | ||
} | ||
|
||
protected function getGenerator(array $params): Generator | ||
{ | ||
for ($i = 0; $i < $params['size']; ++$i) { | ||
yield [$i, sprintf('*%s*', $i)]; | ||
} | ||
} | ||
|
||
protected function getSubject(array $params): Traversable | ||
{ | ||
return new $params['class']($this->getGenerator($params)); | ||
} | ||
|
||
protected function loop(Traversable $input): Generator | ||
{ | ||
foreach ($input as $key => $value) { | ||
yield [$key, $value]; | ||
} | ||
} | ||
|
||
protected function loopUntil(Traversable $input, array $params): Generator | ||
{ | ||
$breakAt = $params['size'] / 2; | ||
|
||
foreach ($input as $key => $value) { | ||
yield $key => $value; | ||
|
||
if (0 === $breakAt--) { | ||
break; | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.