Tag Archive for 'parse'

PHP: Measuring code execution time

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!