Skip to content

Instantly share code, notes, and snippets.

@leocarmo
Created April 26, 2020 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leocarmo/4a1320da1b61523c0c8d00ef2a3524e3 to your computer and use it in GitHub Desktop.
Save leocarmo/4a1320da1b61523c0c8d00ef2a3524e3 to your computer and use it in GitHub Desktop.
<?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