How to check speed of PHP function in wordpress

How to check speed of PHP function in wordpress

If you need to check how many time any PHP function takes you can make use of these simple tricks.

By default, WordPress has some functions to check the speed of overall database queries and number of queries:

$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 the footer and you will see memory and queries count. But what if you want to check speed only for one function or part of the site. For example, you want to compare the speed of different functions. This genius code will help you.

Place before function or code this string:

$time_start = microtime( true );

And after function such record:

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

This will show you the speed of your function in seconds.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.