You are here:  » Display mysql excution time

Support Forum



Display mysql excution time

Submitted by webie on Sun, 2008-02-17 10:32 in

Hi Dave,

I used your code from the dbtest.php script and added it to search.php and displayed this is searchresults.php i get this 1203243816.0106.

How would i format micro seconds into seconds like this (0.23 seconds)

I can't seem to find any code on the web for this hope you can help

Cheers

Darren

Submitted by support on Sun, 2008-02-17 13:24

Hi Darren,

I've just tested the execution time mods with the same functions from the dbtest.php script as follows:

  function timerBegin()
  {
    global $timeBegin;
    list($usec, $sec) = explode(" ", microtime());
    $timeBegin = ((float)$usec + (float)$sec);
  }
  function timerEnd()
  {
    global $timeBegin;
    list($usec, $sec) = explode(" ", microtime());
    $timeEnd = ((float)$usec + (float)$sec);
    return ($timeEnd - $timeBegin);
  }

To use these, wrap the calls to timerBegin() and timerEnd() only around the single line that executes the SQL statement you want to time, so in search.php I now have this code:

    timerBegin();
    $searchresults["numRows"] = database_querySelect($sql,$rows);
    $time = timerEnd();

Now, the $time variable as returned by the timerEnd() function is in seconds already; so i'm not sure how you've come to get a value of 1203243816.0106 - check that you are using the function calls in the same way as me above (and that they are the same functions).

Now to display the value in seconds to 2 decimal places, I normally use sprintf; so what I have added below the above code is this:

    $banner["h2"] .= " [query took ".sprintf("%.2f",$time)." seconds]";

Cheers,
David.