Created
December 10, 2020 13:54
-
-
Save mikkorantalainen/3791bb2d58b8b65155d7ad4897174efb to your computer and use it in GitHub Desktop.
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
Live version: https://3v4l.org/6AABv | |
Test script: | |
<?php | |
$a = array( | |
0 => "foo", | |
1 => "bar", | |
"animal" => "string", | |
3.13 => "pi", | |
); | |
$loops = 1000000; | |
printf("Control loop...\n"); | |
$start = microtime(true); | |
function process_array($a) | |
{ | |
$s = ""; | |
foreach ($a as $key=>$value) | |
{ | |
$s .= "$key=>$value\n"; | |
} | |
} | |
for ($i = 0; $i < $loops; $i++) | |
process_array($a); | |
$elapsed = microtime(true) - $start; | |
printf("Elapsed: %.2f s\n\n", $elapsed); | |
printf("Anonymous lambda function created once and called via reference...\n"); | |
$start = microtime(true); | |
$process_array_lambda = function($a) | |
{ | |
$s = ""; | |
foreach ($a as $key=>$value) | |
{ | |
$s .= "$key=>$value\n"; | |
} | |
}; | |
for ($i = 0; $i < $loops; $i++) | |
$process_array_lambda($a); | |
$elapsed = microtime(true) - $start; | |
printf("Elapsed: %.2f s\n\n", $elapsed); | |
printf("Anonymous lambda function called directly...\n"); | |
$start = microtime(true); | |
for ($i = 0; $i < $loops; $i++) | |
(function($a) | |
{ | |
$s = ""; | |
foreach ($a as $key=>$value) | |
{ | |
$s .= "$key=>$value\n"; | |
} | |
})($a); | |
$elapsed = microtime(true) - $start; | |
printf("Elapsed: %.2f s\n\n", $elapsed); | |
?> | |
Results: | |
PHP version 8.0.0: | |
Control loop... | |
Elapsed: 0.52 s | |
Anonymous lambda function created once and called via reference... | |
Elapsed: 0.53 s | |
Anonymous lambda function called directly... | |
Elapsed: 0.71 s | |
PHP version 7.4.13: | |
Control loop... | |
Elapsed: 0.58 s | |
Anonymous lambda function created once and called via reference... | |
Elapsed: 0.63 s | |
Anonymous lambda function called directly... | |
Elapsed: 0.70 s | |
PHP version 7.4.12: | |
Control loop... | |
Elapsed: 0.57 s | |
Anonymous lambda function created once and called via reference... | |
Elapsed: 0.58 s | |
Anonymous lambda function called directly... | |
Elapsed: 0.62 s | |
PHP version 7.4.11: | |
Control loop... | |
Elapsed: 0.56 s | |
Anonymous lambda function created once and called via reference... | |
Elapsed: 0.52 s | |
Anonymous lambda function called directly... | |
Elapsed: 0.65 s | |
PHP version 7.4.10: | |
Control loop... | |
Elapsed: 0.64 s | |
Anonymous lambda function created once and called via reference... | |
Elapsed: 0.54 s | |
Anonymous lambda function called directly... | |
Elapsed: 0.65 s | |
PHP version 7.4.9: | |
Control loop... | |
Elapsed: 0.53 s | |
Anonymous lambda function created once and called via reference... | |
Elapsed: 0.50 s | |
Anonymous lambda function called directly... | |
Elapsed: 0.63 s | |
PHP version 7.4.8: | |
Control loop... | |
Elapsed: 0.51 s | |
Anonymous lambda function created once and called via reference... | |
Elapsed: 0.53 s | |
Anonymous lambda function called directly... | |
Elapsed: 0.70 s | |
PHP version 7.4.7: | |
Control loop... | |
Elapsed: 0.76 s | |
Anonymous lambda function created once and called via reference... | |
Elapsed: 0.59 s | |
Anonymous lambda function called directly... | |
Elapsed: 0.66 s | |
PHP version 7.4.6: | |
Control loop... | |
Elapsed: 0.52 s | |
Anonymous lambda function created once and called via reference... | |
Elapsed: 0.54 s | |
Anonymous lambda function called directly... | |
Elapsed: 0.67 s | |
PHP version 7.3.25: | |
Control loop... | |
Elapsed: 0.70 s | |
Anonymous lambda function created once and called via reference... | |
Elapsed: 0.61 s | |
Anonymous lambda function called directly... | |
Elapsed: 0.70 s | |
PHP version 7.3.24: | |
Control loop... | |
Elapsed: 0.52 s | |
Anonymous lambda function created once and called via reference... | |
Elapsed: 0.58 s | |
Anonymous lambda function called directly... | |
Elapsed: 0.66 s | |
PHP version 7.3.23: | |
Control loop... | |
Elapsed: 0.72 s | |
Anonymous lambda function created once and called via reference... | |
Elapsed: 0.83 s | |
Anonymous lambda function called directly... | |
Process exited with code 137. | |
PHP version 7.3.22: | |
Control loop... | |
Elapsed: 0.49 s | |
Anonymous lambda function created once and called via reference... | |
Elapsed: 0.60 s | |
Anonymous lambda function called directly... | |
Elapsed: 0.68 s | |
PHP version 7.3.21: | |
Control loop... | |
Elapsed: 0.47 s | |
Anonymous lambda function created once and called via reference... | |
Elapsed: 0.56 s | |
Anonymous lambda function called directly... | |
Elapsed: 0.74 s | |
PHP version 7.3.20: | |
Control loop... | |
Elapsed: 0.54 s | |
Anonymous lambda function created once and called via reference... | |
Elapsed: 0.53 s | |
Anonymous lambda function called directly... | |
Elapsed: 0.66 s | |
PHP version 7.3.19: | |
Control loop... | |
Elapsed: 0.64 s | |
Anonymous lambda function created once and called via reference... | |
Elapsed: 0.86 s | |
Anonymous lambda function called directly... | |
Process exited with code 137. | |
PHP version 7.3.18: | |
Control loop... | |
Elapsed: 0.55 s | |
Anonymous lambda function created once and called via reference... | |
Elapsed: 0.55 s | |
Anonymous lambda function called directly... | |
Elapsed: 0.77 s | |
PHP version 7.2.34: | |
Control loop... | |
Elapsed: 0.87 s | |
Anonymous lambda function created once and called via reference... | |
Elapsed: 0.79 s | |
Anonymous lambda function called directly... | |
Process exited with code 137. | |
PHP version 7.2.33: | |
Control loop... | |
Elapsed: 0.57 s | |
Anonymous lambda function created once and called via reference... | |
Elapsed: 0.62 s | |
Anonymous lambda function called directly... | |
Elapsed: 0.73 s | |
PHP version 7.2.32: | |
Control loop... | |
Elapsed: 0.57 s | |
Anonymous lambda function created once and called via reference... | |
Elapsed: 0.59 s | |
Anonymous lambda function called directly... | |
Elapsed: 0.72 s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment