Jump to content

Archived

This topic is now archived and is closed to further replies.

poirot

Page Execution Time

Recommended Posts

Hello,

I am using a very basic code to calculate execution time:

[code]<?php

$start = microtime();

// Some code here

$exec = microtime() - $start;
echo 'Page Generated in ' . $exec . ' microseconds';

?>[/code]

Is this OK? Oddly enoug, sometimes I get weird negative results [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /] ...

And what would be a good value for that? I have pages in my site that are executed in 0.01 ms, but some others take 0.08 microseconds.

And they do almost the same thing - in fact, it's the same script, but calling different functions. Yet, 8 times slower.

Note: I'm running a local test server; my machine is an athlon 64 3800+, 2gb of ram. Way below the specs of real server then.

Thanks in advance [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]

Share this post


Link to post
Share on other sites
lol..negative results? maybe your computer is so fast it moves backwards in time. Or maybe it's so intuitive that future version of your computer sends the results back in time to present time. [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]

man i dunno why it would do that, to be honest. but i suppose you can throw and if statement in there to catch it. like

[code]
echo "time to generate: ";
if ($exec < 0) {
  echo "instant!"; //..or just 0 or somethin'
} else {
  echo $exec;
}
[/code]

Share this post


Link to post
Share on other sites
I found a quick fix for the negative results.

It may be avoided if I use microtime(true) instead of microtime()
(sets get_as_float) - will return a float.

Unfortunately, this parameter was added as of PHP5. What should I do for older versions?

Share this post


Link to post
Share on other sites
Poirot this may be overkill, but there is a pear package built specifically for benchmarking that you could use.

[a href=\"http://pear.php.net/package/Benchmark\" target=\"_blank\"]http://pear.php.net/package/Benchmark[/a]

I've used it once or twice in the past and it's pretty straightforward. Installation is obviously easy.

Share this post


Link to post
Share on other sites
Thanks Buyocat, but I need something very simple, that I can use whenever I want.

I don't know if that's overkill, but I like to code everything I use, so I can write it "of head" [what's the proper term for this?].

Share this post


Link to post
Share on other sites
Off hand maybe? Off the top of your head? I have no idea. The nice thing about the package is that it is easy to use whenever you want, and you can use it alongside test scripts etc. But I do understand the desire to do things yourself, though I can't say that's a good quality sometimes. Still this is probably an easy enough problem that your solution is just as good.

Share this post


Link to post
Share on other sites
[a href=\"http://us2.php.net/manual/en/function.microtime.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.microtime.php[/a] have an example of getting the exection time in php 4 and 5

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.