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
Remember, the max execution time of a PHP script is 30 seconds.
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.
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.
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.
You can use the microtime
function for this. From the documentation:
microtime
— Return current Unix timestamp with microseconds
If
get_as_float
is set toTRUE
, thenmicrotime()
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With