Take the 2-minute tour ×

I tried using it like this:

$now = microtime(true); 
// cpu expensive code here
echo microtime(true) - $now;   

but regardless of what code I enter between these statements, I alwasy get almost the same results, something like 3.0994415283203E-6

What am I doing wrong?

share|improve this question
Try putting in a sleep(5); call and ensure that the result changes. The time should be at least five seconds. –  El Yobo Jan 25 '11 at 1:32
that works it seems. I get 4.9997820854187 –  Alex Jan 25 '11 at 1:34
Wow! PHP is fast! It evaluates sleep(5); in less than 5 seconds! –  Joel Jan 25 '11 at 1:37
Welcome to scientific notation. –  deceze Jan 25 '11 at 1:39

5 Answers 5

up vote 4 down vote accepted

Better solution. Run the code multiple times to average out the operation:

$runs = 500;

$start = microtime(true);
    //cpu expensive code here
$end = microtime(true);
$elapsed = number_format($end - $start, 4);
$one = number_format(($end - $start) / 500, 7);
echo "500 runs in $elapsed seconds, average of $one seconds per call";
share|improve this answer
that looks much better, thank you :) –  Alex Jan 25 '11 at 1:38
I don't think 5 decimal places will be enough in this case. –  Jonah Jan 25 '11 at 1:38
Then add a 6th or 7th. But I doubt any truly "expensive" code would run in less than e^-5... –  ircmaxell Jan 25 '11 at 1:39
would you consider 0.0005 too much? the problem I'm trying to solve is a shared host that tells me a script of mine is using too much cpu, but don't know which part of it :( –  Alex Jan 25 '11 at 1:42
@Alex: it's completely context dependent. Do you know that there is a bottleneck? Or are you just checking because you think it might be slow? –  ircmaxell Jan 25 '11 at 1:46

3.0994415283203E-6 equates to 0.0000030994415283203.

The E-6 tells you to move the decimal point left six places. E+6 would mean the opposite. As @deceze mentioned, this is called scientific notation.

If you're doing a performance test, it's a good idea to put the code into a 100000 or so iteration loop, and then divide the resulting time by 100000. That way you get a more accurate average.

share|improve this answer

You're not doing anything wrong, it's just that the code you're timing really only takes a fraction of a second to run.

If you want to prove it, sleep for a few seconds.

share|improve this answer

It looks like you are using microtime() without the optional argument, but you say you are, so I am not 100% sure.

What is the output of this:

$now = microtime(true);
echo microtime(true) - $now;
share|improve this answer
The 'true' is the optional argument. –  Joel Jan 25 '11 at 1:36

PHP always amazes me with how fast it is. Your code seems to be right. Maybe your code is really only taking 3 milliseconds.

You could try making a long loop, something like this:

while ($x<1000000)

Add this code inside of your timer. For me, looping 1 million times usually takes about 1/2 second. See if this changes your time.

share|improve this answer
why not use sleep or usleep to delay a measurable amount rather than guessing? –  ircmaxell Jan 25 '11 at 1:36
@ ircmaxell - I don't know. It doesn't matter. This was just the first thing that came to my mind. –  Joel Jan 25 '11 at 1:39

Your Answer


