Created
April 6, 2016 23:30
-
-
Save anytizer/ab7d109a8f20ec01458b399f2e7b1590 to your computer and use it in GitHub Desktop.
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 | |
header('Content-Type: text/plain'); | |
/** | |
* Build an array to loop. | |
*/ | |
$time0 = microtime(true); | |
for($i=9999; $i>0; --$i) | |
{ | |
$values[] = null; | |
} | |
$time1 = microtime(true); | |
$diff1 = $time1 - $time0; | |
/** | |
* Loop's internal logic uses count() in each loop | |
*/ | |
for($i=0; $i<count($values); ++$i){} | |
$time2 = microtime(true); | |
$diff2 = $time2 - $time1; | |
/** | |
* See next 2 lines! Rewrite the above one | |
*/ | |
$total = count($values); | |
for($i=0; $i<$total; ++$i){} | |
$time3 = microtime(true); | |
$diff3 = $time3 - $time2; | |
/** | |
* Sometimes, the difference is 0. | |
*/ | |
$diff3 = $diff3?$diff3:0.00000000001; | |
#echo "\r\nValues Generation: ", $diff1; | |
echo "\r\nfor(\$i=0; \$i<count(\$values); ++\$i): ", $diff2; | |
echo "\r\nfor(\$i=0; \$i<\$total; ++\$i): ", $diff3; | |
echo sprintf("\r\nSpeed Gain: %0.2f times.", $diff2/$diff3); | |
/** | |
* Values Generation: 0.15500903129578 | |
* for($i=0; $i<count($values); ++$i): 0.15700817108154 | |
* for($i=0; $i<$total; ++$i): 0.0060009956359863 | |
* Speed Gain: 26.16 times. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment