Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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