Forum Moderators: coopster

Message Too Old, No Replies

Getting the time taken to run a query from PHP

         

ryan_b83

3:22 pm on Aug 15, 2007 (gmt 0)

10+ Year Member



Hello, I am trying to get the "time taken" to run a mysql query from PHP. I looked on php.net and i could not find a function that returned the time taken to run the last query. Is there any way to get this number?

Thanks,
Ryan

inbound

3:39 pm on Aug 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I find that comparing the time just prior to instructions to the time just after does the job.

e.g.

$time = microtime();
$time = explode(" ", $time);
$time = $time+ $time[0];
$time1 = $time;

CODE THAT NEEDS TIMING

$time = microtime();
$time = explode(" ", $time);
$time = $time[1] + $time[0];
$time2 = $time;

$totaltime = ($time2 - $time1);
echo '<BR>Parsing Time: ' .$totaltime. ' seconds.';

[1][edited by: inbound at 3:45 pm (utc) on Aug. 15, 2007]

d40sithui

3:40 pm on Aug 15, 2007 (gmt 0)

10+ Year Member



u can try the php time() function. run it before and right after you run your query. since its in seconds(int) you can subtract the first value from the second and see how many seconds it took.

inbound

3:43 pm on Aug 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I forgot to add that the above code will measure to quite a few decimal places (ideal for code optimisation), so you end up with timings such as 0.000019 seconds or maybe 1.28234765 etc.

ryan_b83

6:14 pm on Aug 17, 2007 (gmt 0)

10+ Year Member



Awesome, that worked perfect. thanks!