Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accurate way to measure execution times of php scripts

Tags:

php

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 
like image 599
DomingoSL Avatar asked Jun 05 '11 21:06

DomingoSL


People also ask

What is execution time PHP?

Remember, the max execution time of a PHP script is 30 seconds.

How do you calculate execution time?

The difference between the end time and start time is the execution time. Get the execution time by subtracting the start time from the end time.

How do you measure execution?

1) Create a loop around whatneeds to be measured, that executes 10, 100, or 1000 times or more. Measure execution time to the nearest 10 msec. Then divide that time bythe number of times the loop executed. If the loop executed 1000 timesusing a 10 msec clock, you obtain a resolution of 10 µsec for theloop.

What is the default execution time of a PHP script in seconds?

The default execution time is 30 seconds, which may be too small for your needs. If you need to raise this limit, you must create a phprc file.


1 Answers

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; 
like image 99
Tim Cooper Avatar answered Oct 18 '22 16:10

Tim Cooper