2

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");
Dazzle
  • 2,196
  • 3
  • 18
  • 40
  • 7
    Try it and see ;) Also the subject is misleading as nowhere are you actually using microtime inside another microtime. PS: your example above is missing where you declare `$finish`. – IncredibleHat Aug 23 '19 at 16:24

1 Answers1

1

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.

Divey
  • 1,609
  • 1
  • 12
  • 22