Skip to content
Advertisement

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

Advertisement

Answer

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.

Example usage:

$start = microtime(true);
while (...) {

}
$time_elapsed_secs = microtime(true) - $start;
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement