In PHP it possible to use microtime() inside another microtime()?

143 Views Asked by At

I have a loop with a number of functions

I want to check how long the entire loop runs, and also each of the functions within the loop

Is it possible to use a parent and many child microtime functions simultaneously?

$start = microtime(true);

foreach ($rows as $row) {

  if ($row == "a") {
     $start1 = microtime(true);
     // do function stuff
     $finish1 = microtime(true);
     echo("function 1 took", $finish1 - $start1, "seconds");
  }

  if ($row == "b") {
     $start2 = microtime(true);
     // do function stuff
     $finish2 = microtime(true);
     echo("function 2 took", $finish2 - $start2, "seconds");
  }
}

$finish = microtime(true);

echo("all functions took", $finish - $start, "seconds");
1

There are 1 best solutions below

0
Divey On

By the "parent" function, I assume you mean the microtime function call outside the foreach loop.

If that's the case, then yes, each time you call microtime, it will return the current timestamp of the execution of the script, regardless of previous calls to microtime.