1

I'm trying to measure execution time of a function.

I tried the following code snippet

$duration = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
$hours = (int)($duration/60/60);
$minutes = (int)($duration/60)-$hours*60;
$execution_time = $duration - $hours*60*60 - $minutes*60;

And I get 2sec per request

But when I use xhprof, I get 0.9 per request.

Which should I trust and why is there a second difference per request?

Thanks in advance.

  • 1
    Probably because xhprof is profiling your PHP code execution, while your `$_SERVER["REQUEST_TIME_FLOAT"]` adds the overheads for the webserver handling of the request before it's passed on to PHP execution – Mark Baker Dec 01 '14 at 16:03
  • 1
    However, profiling itself is an overhead, so what you're looking for shouldn't be what method of profiling is the fastest; it's the consistency of your profiling method when reporting results – Mark Baker Dec 01 '14 at 16:05
  • once running inside php, microtime() has lower overhead than xhprof. To see the difference, put $start_time = microtime(true) at the top of your landing page, and compute $microtime_duration = microtime(true) - $start_time; – Andras Dec 01 '14 at 19:35

0 Answers0