Measuring the Speed (Execution Time) of a PHP Script

Sometimes you need to measure the execution time of a separate piece of code in PHP. It’s not difficult; below we’ll look at how.

Simple option

To measure the execution speed of some separate part of PHP code, you can use the built-in PHP function microtime( true ). The function will return a UNIX timestamp in microseconds. The true parameter tells the function to return a number, not a string, so that you can later simply subtract one number from another.

The logic is simple: first measure the current time in microseconds, execute the code, measure the current time again, and calculate the time difference — this difference will be the code execution time. Using the sprintf() function, we format the result into a more readable format.

$start = microtime( true );

get_bloginfo( 'homeurl' ); // some code

$diff = sprintf( '%.6f sec.', microtime( true ) - $start );

echo "Execution time: $diff"; // Execution time: 0.000014 sec.

A more complex option, but with additional functionality

To measure the speed of running PHP code, at some point I wrote such a function:

/**
 * Calculates PHP code execution time (in seconds).
 *
 *     exec_time();
 *     // code
 *     echo exec_time('get') .' sec.'; //> 0.03654 sec.
 *
 * @version 3.4.5
 *
 * @param string $phase       run             - enables counting: first time or after a pause (default).
 *                            start           - clears all caches and enables counting - clear & run.
 *                            get             - gets the difference between the previous function call.
 *                            getall|end|stop - gets the difference between the first function call (run).
 *                            pause           - temporarily stops counting. exec_time() to continue.
 *                            clear           - completely clears the result. exec_time() to start a new counting.
 * @param int    $round       Round the result to how many digits after the decimal point.
 *
 * @return float|void Example 0.03654
 */
function exec_time( $phase = 'run', $round = 6 ){
	static $prev_time, $collect;

	if( 'start' === $phase ){
		$collect = $prev_time = 0;
	}
	elseif( 'clear' === $phase ){
		return $collect = $prev_time = 0;
	}

	list( $usec, $sec ) = explode( ' ', microtime() );
	$mctime = bcadd( $usec, $sec, 8 );

	if( $prev_time ){
		$exectime = $mctime - $prev_time; // bcsub( $mctime, $prev_time, 8 );
		$collect  += $exectime; // bcadd( $collect, $exectime, 8 );
	}
	$prev_time = $mctime;

	if( 'pause' === $phase ){
		$prev_time = 0;
	}
	elseif( 'get' === $phase ){
		return round( $exectime, $round );
	}
	elseif( false !== strpos( 'getall end stop', $phase ) ){
		return round( $collect, $round );
	}
}

If you measure separate fast functions, for example: __(), then you can repeat the execution for clarity. For example, measure the operation of __():

exec_time();

for( $i=1; $i<50000; $i++ ){
	$r = __( 'Settings', 'default' );
}

echo exec_time('get') .'sec.'; // 0.51650 sec.

--

Also see WP code execution time counting functions: