The following code measures the amount of time it takes to execute a chunk of code.
In PHP 4 (source):
$time1 = explode(' ', microtime()); /* your chunk of code here */ $time2 = explode(' ', microtime()); $begintime = $time1[1] + $time1[0]; $endtime = $time2[1] + $time2[0]; $totaltime = $endtime - $begintime; echo "\n<!-- Executed in $totaltime sec -->\n";
In PHP 5 (source):
$time_start = microtime(true); /* your chunk of code here */ $time_end = microtime(true); $time = $time_end - $time_start; echo "\n<!-- Executed in $time sec -->\n";
If you liked this post, please subscribe to my feed. Thanks for visiting!


Minor thing, but for the php4 version, to get an accurate time, the $begintime = $time[1] + $time[0]; (the first one) should come after the 2nd microtime call. Just change $time to $time1/$time2 so you can work with them after the measurement.
I know I know, I’m way too concerned with the details
Good point. The benchmark is being polluted by an extra “$begintime = $time[1] + $time[0];” *gasp*
Seriously, thanks for pointing that out.
That code seems to have floated around the intarwebs for many years without anyone noticing. Shame!
Welcome to my humble experiment of a blog, and Zenphoto banzai!