For a very simple profiling I use microtime() like this:

$now = microtime();
for (...) {
    // do something
    echo microtime() - $now;
    $now = microtime();
}

Now, the output of the echo line seems completely random, that is, I expected fluctuations, but I don't expected negative numbers showing up.

However, a typical result contains ~ 1/3 negative numbers. I confirmed this on Solaris (PHP 5.0.x) and WinVista (PHP 5.2.3).

What the heck is going on here? Have I invented accidently a time machine?

Comments

Yes U have!!!!! :) Let me know to control it....

Written by OM The Eternity

this is something i noticed too but never bothered investigating, i'm curious for the anwser :)

Written by ChrisR

Accepted Answer

If you want to do operations on what is returned by microtime, you have to set the "get as float" parameter to true (it defaults to false).

http://www.php.net/manual/en/function.microtime.php

$now = microtime(true);
for (...) {
    // do something
    echo microtime(true) - $now;
    $now = microtime(true);
}
Written by Arkh
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki