Skip to content

Instantly share code, notes, and snippets.

@divinity76
Last active March 30, 2022 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save divinity76/076b84226361fe3932a5ff37e2d34925 to your computer and use it in GitHub Desktop.
Save divinity76/076b84226361fe3932a5ff37e2d34925 to your computer and use it in GitHub Desktop.
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