Created
February 9, 2016 15:30
-
-
Save rmasters/eed942eb4c8cc1bd96c0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
function hashGenerator($items) { | |
for ($i = 0; $i < $items; $i++) { | |
yield rtrim(base64_encode(md5(microtime())),"=") => rtrim(base64_encode(md5(microtime())),"="); | |
} | |
} | |
function time_fn($name, callable $fn, ...$args) { | |
$start = microtime(true); | |
$res = call_user_func_array($fn, $args); | |
printf("%s: %.3f\n", $name, microtime(true) - $start); | |
return $res; | |
} | |
printf("PHP %s\n", phpversion()); | |
$iteration = $argc >= 2 ? $argv[1] : false; | |
$count = $argc >= 3 ? $argv[2] : 100000; | |
if ($iteration == "while") { | |
$hash = hashGenerator($count); | |
time_fn("While iteration", function($hash) { | |
while (list($key, $value) = each($hash)); | |
}, $hash); | |
} else if ($iteration == "foreach") { | |
$hash = hashGenerator($count); | |
time_fn("Foreach iteration", function($hash) { | |
foreach ($hash as $key => $value); | |
}, $hash); | |
} else { | |
print("Usage: {$argv[0]} (while|foreach)\n"); | |
exit(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment