Heads up! The article doesn’t mean the CPU Execution Time but the clock time and so it can vary when comparing with the “max_execution_time”.

Tracking the PHP script execution time is very important in case you’re dealing with a large number of data. The more execution time, the slower the performance of the system. It’s especially important when querying large data from the database.

Once you created a function, you can measure its time execution while calling it. Let’s see an example of get_posts() in WordPress because it can contain huge data based on the number of posts you have.

$time_start = microtime(true);

$posts = get_posts();

$time_end = microtime(true);

$execution_time = $time_end - $time_start;

error_log( print_r( 'Execution Time For get_posts(): ' . $execution_time, true ) );

You’ll get execution time in seconds. If you get weird-looking results, format the execution time:

error_log( print_r( 'Execution Time For get_posts(): ' . number_format( (float) $execution_time, 10 ), true ) );

In case you don’t already know, you can Log PHP data in JavaScript Console.

I hope this information is useful!

Tracking the PHP script execution time
Tagged on:     

Sanjeev Aryal

Don't bury your thoughts, put your vision into reality ~ Bob Marley.

Leave a Reply

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