How to Measure Script Execution Time in PHP
The time that is required for executing a PHP script, is known as script execution time.
It is recommended to use a clock for calculating it. If you know the time before the script execution and after it, you will manage to get the execution time easily.
Let’s take a look at a script sample:
for ($i = 1; $i 1000; $i++) < echo "Hello W3docs!"; > ?>
Getting the Clock Time with microtime()
The microtime() function is used for getting the clock time. First, it should be used before the script starts, then, at the end of it. Afterwards, the formula (End_time – Start_time) should be used.
With the help of the microtime() function, time is returned in seconds. The execution time is not fixed, it is decided by the processor.
To be more precise, let’s see an example:
// Starting clock time in seconds $start_time = microtime(true); $a = 1; // Start loop for ($i = 1; $i 10000000; $i++) < $a++; > // End clock time in seconds $end_time = microtime(true); // Calculating the script execution time $execution_time = $end_time - $start_time; echo " Execution time of script = " . $execution_time . " sec"; ?>
Execution time of script = 1.4305651187897 sec
Describing the microtime() Function
The microtime() function of PHP is used for returning the current Unix timestamp. It returns a string in microseconds.
Passing a boolean value as a parameter of this method will return the current time in seconds.
Accurate way to measure execution times of php scripts
I want to know how many milliseconds a PHP for-loop takes to execute. I know the structure of a generic algorithm, but no idea how to implement it in PHP:
Begin init1 = timer(); // where timer() is the amount of milliseconds from midnight the loop begin some code the loop end total = timer() - init1; End
You can fiddle around with swaths of microtime() statements if you need this in production, but if it’s just for testing, just use xdebug’s profiler for instance. No messy code is a real plus.
17 Answers 17
You can use the microtime function for this. From the documentation:
microtime — Return current Unix timestamp with microseconds
If get_as_float is set to TRUE , then microtime() returns a float, which represents the current time in seconds since the Unix epoch accurate to the nearest microsecond.
$start = microtime(true); while (. ) < >$time_elapsed_secs = microtime(true) - $start;
I know this is waaaaaay too late (almost 4 years), but as a comment. using these calculations (with the parameter get_as_float as true ) will give you results in seconds, according to PHP documentation.
@patrick and that’s what I said: if get_as_float is true , microtime() returns the value representing the seconds.
While this is a very old post, we should refer to the fact that the float returned does contain the microseconds. Best solution is to times this by 1000. see stackoverflow.com/questions/3656713/… as an example..
«For performance measurements, using hrtime() is recommended.» php.net/manual/en/function.hrtime.php (PHP 7 >= 7.3.0, PHP 8)
You can use microtime(true) with following manners:
Put this at the start of your php file:
//place this before any script you want to calculate time $time_start = microtime(true);
// your script code goes here
Put this at the end of your php file:
// Display Script End time $time_end = microtime(true); //dividing with 60 will give the execution time in minutes, otherwise seconds $execution_time = ($time_end - $time_start)/60; //execution time of the script echo 'Total Execution Time: '.$execution_time.' Mins';
It will output you result in minutes .
You can use REQUEST_TIME from the $_SERVER superglobal array. From the documentation:
REQUEST_TIME
The timestamp of the start of the request. (Available since PHP 5.1.0.)REQUEST_TIME_FLOAT
The timestamp of the start of the request, with microsecond precision. (Available since PHP 5.4.0.)
This way you don’t need to save a timestamp at the beginning of your script. You can simply do:
Here, $time would contain the time elapsed since the start of the script in seconds, with microseconds precision (eg. 1.341 for 1 second and 341 microseconds)
More info:
According to the linked documentation, $time will contain the difference in seconds, not in microseconds.
@WimDeblauwe To clarify, yes the result is in seconds. But with microsecond precision. e.g. 1.1 equates to 1 second + 100 microseconds.
time_start= microtime(true); > public function __destruct()< $this->time_end = microtime(true); $this->time = $this->time_end - $this->time_start; echo "Loaded in $this->time seconds\n"; > >
When page is loaded at the end there will be written «Loaded in x seconds»
Neat! But if you don’t explicitly destroy your object, the output would appear after the closing