Last active
March 30, 2022 01:29
-
-
Save divinity76/076b84226361fe3932a5ff37e2d34925 to your computer and use it in GitHub Desktop.
increment benchmark php (pre-increment vs post-increment)
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
hans@xDevAd:~$ php increment_benchmark.php | |
pre-increment won! | |
diff in seconds: 0.1974422932 | |
hans@xDevAd:~$ php increment_benchmark.php | |
pre-increment won! | |
diff in seconds: 0.1974339485 | |
hans@xDevAd:~$ php increment_benchmark.php | |
pre-increment won! | |
diff in seconds: 0.1987068653 | |
hans@xDevAd:~$ php increment_benchmark.php | |
pre-increment won! | |
diff in seconds: 0.2080159187 | |
hans@xDevAd:~$ php increment_benchmark.php | |
pre-increment won! | |
diff in seconds: 0.2066819668 | |
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 | |
declare(strict_types = 1); | |
// first warm up the cpu (if the cpu use powersaving, most cpus do) | |
for($i=0;$i<100_000;++$i); | |
for($i=0;$i<100_000;$i++); | |
// initialize mt1 | |
$mt1=microtime(true); | |
// pre-increment benchmark: | |
$mt1=microtime(true); | |
for($i=0;$i<100_000_000;++$i); | |
$mt1 = microtime(true) - $mt1; | |
// initialize mt2 | |
$mt2=microtime(true); | |
// post-increment benchmark: | |
$mt2=microtime(true); | |
for($i=0;$i<100_000_000; $i++); | |
$mt2 = microtime(true) - $mt2; | |
// benchmark complete | |
if($mt1 === $mt2){ | |
echo "it's a tie!"; | |
}elseif($mt1 > $mt2){ | |
echo "post-increment won!"; | |
}else{ | |
echo "pre-increment won!"; | |
} | |
echo "\n"; | |
echo "diff in seconds: ".number_format(abs($mt1-$mt2),10); | |
echo "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment