Calculate the time required to perform a specific function

I can do it? eg. if i want to check if str_replace() faster than preg_replace() ?

+9
performance function php
source share
3 answers

You can run the same line 10,000 times (or more) in your script and use microtime(true) to indicate the time it took.

Link: microtime ()

+15
source share

A simple way:

 $time = microtime(true); // time in Microseconds // Your code here echo (microtime(true) - $time) . ' elapsed'; 

The hard (er) way: Use the code profiler to find out how long your methods will take.

+28
source share

I found this "bisko" answer in this thread.

$ start = microtime (true);

for (...) {....}

$ end = microtime (true);

echo ($ end - $ start). 'seconds;

The for loop can be replaced at any time.

+6
source share

All Articles