Track Execution Time in PHP

Performing benchmark testing in PHP by tracking the execution time of pieces of code.

1552 views
d

By. Jacob

Edited: 2021-04-27 17:58

Track execution time, PHP

To track the execution time of a PHP script—or just a part of some PHP code—we should make a timestamp right before executing the relevant code, and then again right after the code finishes.

This is quite useful for performing simple benchmark testing of code. Often we will find that there is multiple ways to do the same thing when programming, and this is also true when we are coding in PHP. Some PHP functions are simply faster than others.

For the most part, the speed difference is not going to matter unless you are handling several thousands—or even millions—of requests. But, if we know something is faster, then why not use the most efficient method from the beginning? Very often, it is actually obvious what will be the fastest way.

A simple speed test can be performed like this:

$start_time = microtime(true);

// Your code here

$time_spent = microtime(true)-$start_time;

echo $time_spent;

When the parameter of microtime is set to true, the result will be returned in seconds. If you leave out the parameter, a string will be returned instead of a float, which can not be used easily in calculations; hence why microtime(true) is used instead of clean microtime().

Determining which is faster

In many cases, a single run is not to be enough for us to tell which piece of code is the fastest, since the difference is minuscule.

So, in order to notice a difference, we should combine this with a while or for loop, and then run the test at least a million times:

$start_time = microtime(true);

for ($i = 0; $x <= 1000000; $i++) {
  // Your code here
}

$time_spent = microtime(true)-$start_time;

echo $time_spent;

Using the time command in Linux

There is also a command called time available on Linux systems that can be used to exemine the execution time of programs and scripts run from a terminal; to use the command you simply place it in front of whatever command and arguments that you wish to run:

time php name_of_script.php

This should output the total time elapsed while executing a given program:

real    0m0.418s
user    0m0.104s
sys     0m0.314s

The real value represents the total execution time.

Tell us what you think:

  1. In this Tutorial, it is shown how to redirect all HTTP requests to a index.php file using htaccess or Apache configuration files.
  2. How to create a router in PHP to handle different request types, paths, and request parameters.
  3. Tutorial on how to use proxy servers with cURL and PHP
  4. When using file_get_contents to perform HTTP requests, the server response headers is stored in a reserved variable after each successful request; we can iterate over this when we need to access individual response headers.
  5. How to effectively use variables within strings to insert bits of data where needed.

More in: PHP Tutorials