3

I am trying to calculate the time it takes a query to run using a php script:

For example:

$sql = "SELECT COUNT(*) FROM `order_items` LEFT JOIN `orders` ON `oi_o_id` = `o_id` WHERE `o_status` = 'completed' AND `oi_p_id` = '10' LIMIT 1";
$sqlStart = getMicroTime();
$result = mysql_query($sql);
$sqlEnd = getMicroTime();
$sqlTime = $sqlEnd - $sqlStart;

echo $sqlTime;

function getMicroTime() {
    list($msec, $sec) = explode(' ', microtime());
    return floor($sec / 1000) + $msec;
}

This appears to work most of the time, but occasionally I get a negative value e.g. -0.98840499995276. How is this possible? and is there a better way to get the query execution time?

Thanks

4
  • Don't seem to ever get a negative number: codepad.org/rL6MWCoK Commented Nov 16, 2011 at 17:41
  • Unfortunately I do, thanks for Marc B for actually answering the question Commented Nov 16, 2011 at 17:46
  • @Neal Actually it does fail - I just increased to 1000 iterations and it failed on around 986 codepad.org/OVf9aoOR Commented Nov 16, 2011 at 18:05
  • It'd appear to be a timing glitch of some sort. Some minor mods to @Lizard's version and there's no negative times: codepad.org/UetGzU3l Commented Nov 16, 2011 at 19:55

3 Answers 3

8

Your microtime function is not necessary, simply do

$start = microtime(TRUE);
... do query
$end = microtime(TRUE);

passing in the TRUE value has microtime() return the timestamp as an actual float, not that moronic string format that some [truly nasty but accurate description of said person's intelligence] thought would be a useful way of returning the data.

Sign up to request clarification or add additional context in comments.

5 Comments

With microtime() false this - $time = $time_end - $time_start; echo "Did nothing in $time_end - $time_start=$time seconds</br>"; produces this output - but somehow PHP got the math right - any idea what its actually is doing here? Did nothing in 0.55040500 1321470419 - 0.55024300 1321470419=0.000162 seconds
with false, it returns a string. substracting two strings is essentially meaningless.
I agree - but when I tried it - it appears to work. Thats the strange thing.
because PHP will use the first number-looking bit it finds, which happens to be the msec value. If the start is '987 456' and the end is '123 457' (real diff: 0.136 sec), you'll end up doing 123-987 = -864.
I understand the OP's problem - I am wonder why its working in php 5.3.6. Try this $time_start = microtime(false); usleep(100); $time_end = microtime(false); $time = $time_end - $time_start; echo "Did nothing in $time_end - $time_start=$time seconds</br>"; It is producing the correct result somehow
5

Sometimes PHP use the 'E notation' to represent floating point numbers: if you wish to avoid this, you can use the round function in this way:

$start_time = (float) round(microtime(true) * 10000,0);
{your code here}
$finish_time = (float) round(microtime(true) * 10000,0);
$time = ($finish_time - $start_time) / 10000;

Comments

1

Hmm works for me - PHP manual does say this. Have you checked for subspace time anomalies :-)

This function is only available on operating systems that support the gettimeofday() system call. 

Also tried the alternative method from PHP manual

$time_start = microtime(true);

// Sleep for a while
usleep(100);

$time_end = microtime(true);
$time = $time_end - $time_start;

echo "Did nothing in $time seconds\n";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.