What is best way to get the time it took for Php MySQL to process a Query?

Hallo,

What is best way to get the time it took for Php MySQL to process a Query?
I am doing this currently:
1- When the Query is POSTED I set:
$t1 = $_SERVER[‘REQUEST_TIME’];

2- When the Query is done, that is after:
mysql_query($sql_chk_url) or die(mysql_error());
I set:
$t2 = $_SERVER[‘REQUEST_TIME’];

However, $t1 $t2 are always the same even though it takes like 5 seconds for the page to come back vs 1 second. etc.

What to do?

Thanks.

$_SERVER[‘REQUEST_TIME’]; will display the time in which the request was made, not the current time… You could run a difference between that and time() at the very end of your script for a full page process time. Use time() before and after your query for your query process time.

Hi,

I switched to time(); for getting $t1 & $t2, but I still see a difference of 0.000
between the 2!

Bah, ok microtime() then… See examples on http://php.net/manual/en/function.microtime.php

I did as you suggested.
That is now I have at the start of the page:
$t1 = microtime();

and at the end of the page after the Query is down and the page generated:

$t2 = microtime();

however, some of the time I get a Negative value! What the HEK!

This should be super simple, but actually microtime requires a careful reading of the manual.

Return Values

By default, microtime() returns a string in the form “msec sec” … If get_as_float is set to TRUE, then microtime() returns a float

So microtime() is useless for any kind of math, and you should almost always use microtime(true).

Hi,

That seems to work fine. That is microtime(true) results in more negative time diffs.

ThanX :slight_smile: