Jump to content

Page Execution Time


poirot

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\" /]
Link to comment
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]
Link to comment
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?
Link to comment
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.
Link to comment
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?].
Link to comment
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.
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.