Sometimes, we want to track the script execution time in PHP.
In this article, we’ll look at how to track the script execution time in PHP.
How to track the script execution time in PHP?
To track the script execution time in PHP, we can use the microtime
function.
For instance, we write
$time_start = microtime(true);
for ($i = 0;$i < 1000;$i++) {
//...
}
$time_end = microtime(true);
$execution_time = ($time_end - $time_start) / 60;
echo '<b>Total Execution Time:</b> ' . $execution_time . ' Mins';
to call microtime
with true
to return the $time_start
timestamp.
And then we run some code that we want to measure with
for ($i = 0;$i < 1000;$i++) {
//...
}
Then we call microtime
again after the loop to get the timestamp of the time that the loop finished running.
Then we subtract $time_end
by $time_start
to get the time difference in seconds.
And we divide the time by 60 to get the time in minutes.
Conclusion
To track the script execution time in PHP, we can use the microtime
function.