Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
increment benchmark php (pre-increment vs post-increment)
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
<?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