timer_float()WP 5.8.0

Get the time elapsed so far during this PHP script.

Uses REQUEST_TIME_FLOAT that appeared in PHP 5.4.0.

No Hooks.


float. Seconds since the PHP script started.





#1 Several challenges

Let's see how the function works if it is called several times:

var_dump( timer_float() );  // float(0.069790124893188)

sleep( 1 );

var_dump( timer_float() );  // float(1.0698711872101)

#2 Format the value

An example of how to format the result of a function into a normal human form.

$timer = timer_float();

printf( 'Passed %.4F sec.', $timer ); //> It has been 0.1347 sec.

#3 Get the time from the start of PHP to the start of WordPress

This function gives us the time from when PHP is running and timer_stop() from when WP core is running, which means we can calculate the difference and see how much time passes before WP itself starts.

if( isset( $_GET['check_time'] ) ){
	add_filter( 'init', 'my_check_time', PHP_INT_MAX );

function my_check_time(){

	$timer_float = timer_float();
	$timer_stop = timer_stop( 0, 15 );

	var_dump( $timer_float ); //       float(0.036677122116089)
	var_dump( $timer_stop );  // string(17) "0.034716844558716"

	var_dump( $timer_float - (float) $timer_stop );  // float(0.0019602775573729)


As you can see, from the start of PHP to the start of WP, it takes 0.00196 seconds. I thought it would be much faster 🙁

I ran this code on the local WordPress 5.9.


#4 Get the time from PHP startup to WP kernel loading

On the wp_loaded hook you can say that the entire WP core is loaded, but no database queries have yet been made to handle the current URL request.

if( isset( $_GET['check_time'] ) ){
	add_filter( 'wp_loaded', 'my_check_time', PHP_INT_MAX );

function my_check_time(){

	var_dump( timer_float() );  // float(0.035823106765747)


As you can see it took 0.0358 seconds to load the WP kernel since PHP was running.

I ran this code on a local WordPress 5.9 (bare installation without plugins)


#5 Output page generation time, the number of requests and how much memory was used

Display it all in the footer of the site (in the front) and in the footer of the admin panel:

 * Outputs data about the number of queries to the database,
 * the script execution time and the amount of memory used.
add_filter( 'wp_footer', 'wp_usage' ); // on the site
add_filter( 'admin_footer_text', 'wp_usage' ); // in the admin

function wp_usage(){

	echo sprintf(
		'SQL: %d for %.3F sec. %s MB',
		round( memory_get_peak_usage()/1024/1024, 2 )

#6 Measure the execution time of a piece of code in pure PHP

For this operation, we don't need any WP helper functions. The native PHP function microtime() is enough here.

Let's measure how long the get_posts() function takes to retrieve the posts:

$time_start = microtime( true );

// the code, the execution time of which is measured

$diff = sprintf( '%.7f', microtime( true ) - $time_start );

echo "get_posts() worked $diff seconds";

 we get:
 get_posts() worked for 0.0030319690704346 seconds


Since 5.8.0 Introduced.

timer_float() code WP 6.5.2

function timer_float() {
	return microtime( true ) - $_SERVER['REQUEST_TIME_FLOAT'];