Calculate the execution time of a method in PHP
measure php execution time
$start = microtime(true);
while (...) {
}
$time_elapsed_secs = microtime(true) - $start;
Clock time can get using microtime() function. First use it before starts the script and then at the end of the script. Then using formula (End_time – Start_time). The mirotime() function returns time in seconds.
<?php
// Starting clock time in seconds
$start_time = microtime(true);
$a=1;
// Start loop
for($i = 1; $i <=1000; $i++)
{
$a++;
}
// End clock time in seconds
$end_time = microtime(true);
// Calculate script execution time
$execution_time = ($end_time - $start_time);
echo " Execution time of script = ".$execution_time." sec";
?>
Output
Execution time of script = 1.6927719116211E-5 sec