1

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?

Alex
  • 60,472
  • 154
  • 401
  • 592

5 Answers5

6

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

$runs = 500;

$start = microtime(true);
for ($i = 0; $i < $runs; $i++) {
    //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";
ircmaxell
  • 155,647
  • 33
  • 256
  • 309
  • I don't think 5 decimal places will be enough in this case. – Jonah Jan 25 '11 at 01: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 01: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 01: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 01:46
  • I'm checking all the functions that I suspect they use more cpu. – Alex Jan 25 '11 at 01:48
  • @Alex: but is that because it's slow, or because you want to make it as efficient as possible? – ircmaxell Jan 25 '11 at 01:50
  • the host tells me it's slow :) yes I want to make it as efficient as possible – Alex Jan 25 '11 at 01:57
  • @Alex: Then start by profiling the entire script first. Then you have a baseline to compare to. Otherwise you'll have a hard time determining what to do, and what effect it will have... – ircmaxell Jan 25 '11 at 02:00
3

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.

Jonah
  • 9,535
  • 5
  • 39
  • 74
1

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.

ircmaxell
  • 155,647
  • 33
  • 256
  • 309
Dan Grossman
  • 49,405
  • 10
  • 105
  • 95
1

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);
sleep(1);
echo microtime(true) - $now;
sberry
  • 113,858
  • 17
  • 127
  • 157
0

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:

$x=0;
while ($x<1000000)
  {
  $x++;
  }

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.

Joel
  • 2,439
  • 6
  • 29
  • 43