How to check speed of separate function in wordpress

Any developer of theme or plugin on php sometimes needs to check speed of custom function or even a part of code. Here simple solution. 

By default, wordpress has some functions to check speed of overall database queries and number of queries.

Code for this

$user = wp_get_current_user();
if ( $user->id == 1 ) {
echo " MySQL: " . get_num_queries() . " queries for "; timer_stop(1);
echo " seconds. Memory: ".round(memory_get_usage()/1024/1024, 2)." MB";

Place this code in footer and you will  see memory and queries count. But what if you want to check speed only for one function or part of site. For example, you want to compare speed of different functions. This genius code will help you

Place before function or code

$time_start = microtime( true );

Place after function

echo number_format( microtime( true ) - $time_start, 10 );

This will show you speed of your function in microseconds.

3.3/5 - (11 votes)

We will be happy to hear your thoughts

Leave a reply

Wordpress optimization, monetizing tips and tricks