Closed issei-m closed 7 years ago
This PR changes toArray() logic using built-in iterator_to_array function. Bringing a little performance improvement without changing behavior against previous version.
toArray()
iterator_to_array
This time, I simply tested with following snippet:
<?php require __DIR__ . '/vendor/autoload.php'; define('ITEMS_COUNT', 100000); define('TRY_COUNT', 100); $grandchildren = array_fill(0, ITEMS_COUNT, 'grand child'); $grandchildren[] = array_fill(0, ITEMS_COUNT, 'great grandchild'); // with Array $array = array_fill(0, ITEMS_COUNT, 'x'); $array[] = $grandchildren; $coll = \DusanKasan\Knapsack\Collection::from($array); $st = microtime(true); for ($i = 0; $i < TRY_COUNT; $i++) $coll->toArray(); printf("with array -> AVG: %.6f [s]\n", (microtime(true) - $st) / TRY_COUNT); // with Generator $generator = function () use ($grandchildren) { for ($i = 0; $i < ITEMS_COUNT; $i++) { yield 'x'; } yield $grandchildren; }; $coll = \DusanKasan\Knapsack\Collection::from($generator); $st = microtime(true); for ($i = 0; $i < TRY_COUNT; $i++) $coll->toArray(); printf("with Generator -> AVG: %.6f [s]\n", (microtime(true) - $st) / TRY_COUNT); // show memory usage printf("Peak Memory Usage : %.3f [MB]\n", memory_get_peak_usage() / (1024 * 1024));
before
with array -> AVG: 0.022309 [s] with Generator -> AVG: 0.202728 [s] Peak Memory Usage : 21.252 [MB]
after
with array -> AVG: 0.008613 [s] with Generator -> AVG: 0.174608 [s] Peak Memory Usage : 21.252 [MB]
For generator is not very improved but for array is 2x+ faster.
Thank you for the contribution!
This PR changes
toArray()
logic using built-initerator_to_array
function. Bringing a little performance improvement without changing behavior against previous version.This time, I simply tested with following snippet:
before
after
For generator is not very improved but for array is 2x+ faster.