Created
April 29, 2019 22:12
-
-
Save kresnasatya/0c7e49e0a5185ea9d3099177bed4f9d1 to your computer and use it in GitHub Desktop.
Sample measure speed of code in PHP
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 | |
$start = microtime(true); | |
$numbers = array(1, 2, 3, 4, 5); | |
$new_numbers = array_map(function ($num) { | |
return $num * $num; | |
}, $numbers); | |
print_r($new_numbers); | |
$end = microtime(true); | |
$duration = round($end - $start * 1000); | |
echo "Duration: $duration seconds"; | |
$second_start = microtime(true); | |
$second_numbers = array(1, 2, 3, 4, 5); | |
$new_second_numbers = array(); | |
foreach ($second_numbers as $number) { | |
$new_second_numbers[] = $number * $number; | |
} | |
print_r($new_second_numbers); | |
$second_end = microtime(true); | |
$second_duration = round($second_end - $second_start * 1000, 2); | |
echo "Second duration: $second_duration seconds"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment