Skip to main content

Measure Execution Time of Your PHP Script

I have seen lots of expert advice on how to measure the execution time of php script. First, if you need to analyze execution time of big chunk of code then I think using a decent profiler tool is a good idea. But….If you quickly analyze execution time of a chunk of the code then? Well, there are multiple ways to calculate script execution time of particular code block. And this type of situation arises if you have multiple alternative codes to do the same thing. Hence in this post, I will explain what I use to do when I got trapped in this situation.

This is totally experimental and does not gurantee the best way.

Ways to calculate execution time of php script

  1. Difference of microtime
  2. Using PHP function getrusage

Difference of Microtime for PHP Script Execution

If you will search on Google for how to calculate the execution time of your PHP script then probably this method is the first thing which you will get. So what is the microtime ways to calculate php script execution time? Well, this is the easiest way. All you need to do is to store get the difference of microtime between starting and end point of your code.

For example:
<?php
$timer = microtime(true);
sleep(2);
print $total_time = microtime(TRUE) - $timer;//Print 2.0034
?>

In above example at line 1 $timer = microtime(true) first stores current microsecond value as float in $timer. Then just written sleep(2) to halt executation for 2 seond and then taken another microtime and calculated the difference.

Since this is the easiest and as first come in our mind as logic solution we take microtime route to calculate the script execution time.

Using PHP Function getrusage to Calculate Script Execution Time

PHP provide a function with name getrusage which returns current resource used by PHP. But the difference is that it gives the actual CPU time based on user and system. So for example, if you want to calculate the user CPU time or the system CPU time then you should use the php getrusage function.

For example for the total CPU time of the same sleep(2) is

<?php
sleep(2);
$ru = getrusage();
print ($ru['ru_utime.tv_usec'] + $ru['ru_stime.tv_usec'])/1000000;
?>

So When to mcrotime and when to getrusage?

If you will compare both microtime approach and getrusage then you will find lots of difference. This is because getrusage calculate actual CPU time of both user and system. However using microtime you can get actual wall time of the script execution.

Suitable condition to use microtime

  • To calculate the database connection and time in actual query execution.
  • For REST or SOAP call to a third party API.

Suitable Conditions to use getrusage

  • Any same server file manipulation
  • Long looping.
  •  High load server calls

What if you need time taken by a big bunch of script

Above are the scenarios where you have calculated the time for a very short bunch of code. Now, if you have a complete framework and want to analyze the wall time of all php execution of particular request?

Will you put a timer at first and the last PHP file and calculate timing?

No, you can do it easily as well. All you need to do is to put a timer at the beginning and use register_shoutdown_function which will get executed just before ending all script execution.

For example:
<?php
define('TIMER', microtime(true));
register_shutdown_function('calculate_time');
function calculate_time(){
print microtime(true)-TIMER;
}
sleep(2);
?>

By this way you can eaisly calculate complete script time of your WordPress or Magento or any opensource tools.

 

Support Me by Sharing This Article

Ankur Kumar Singh

I am a PHP programmer having some knowledge about Linux. I am always interested in web development and knowledge sharing. I am full time tech evangelist part time human being. :-)

Leave a comment/Ask Question

shares