Created
April 26, 2020 16:53
-
-
Save leocarmo/4a1320da1b61523c0c8d00ef2a3524e3 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
<?php | |
$numbers = range(0, 1_000_000); | |
$test_times = 1_000; | |
$without_results = []; | |
$with_results = []; | |
// Job | |
function cube($number) { | |
return ($number * $number * $number); | |
} | |
// Without natives | |
for ($i = 0; $i < $test_times; $i++) { | |
$start = microtime(true); | |
$cubes = []; | |
foreach($numbers as $number) { | |
$cubes[] = cube($number); | |
} | |
$without_results[] = round((microtime(true) - $start) * 1000); | |
unset($cubes); | |
} | |
// With natives | |
for ($i = 0; $i < $test_times; $i++) { | |
$start = microtime(true); | |
$cubes = array_map('cube', $numbers); | |
$with_results[] = round((microtime(true) - $start) * 1000); | |
unset($cubes); | |
} | |
echo '## Without natives' . PHP_EOL; | |
echo 'Max: ' . max($without_results) . 'ms' . PHP_EOL; | |
echo 'Min: ' . min($without_results) . 'ms' . PHP_EOL; | |
echo 'Avg: ' . (array_sum($without_results) / count($without_results)) . 'ms' . PHP_EOL . PHP_EOL; | |
echo '## With natives' . PHP_EOL; | |
echo 'Max: ' . max($with_results) . 'ms' . PHP_EOL; | |
echo 'Min: ' . min($with_results) . 'ms' . PHP_EOL; | |
echo 'Avg: ' . (array_sum($with_results) / count($with_results)) . 'ms' . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment