Reputation: 77
I'm using PDO, is there any way, to measure how long does it take to complete the query?
Upvotes: 2
Views: 1993
Reputation: 15579
have you tried using Micro Time to measure the time. Just surround whatever you want to measure around this piece of code and you should get the time.
$time_start = microtime_float();
// code to be measured
$time_end = microtime_float();
$timeMeasured = $time_end - $time_start;
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
Upvotes: 1
Reputation: 85496
As Sietse said, use the microtime function to measure the elapsed microseconds.
If you're testing MySQL before the test query, execute the following query:
RESET QUERY CACHE;
Otherwise, you can't be sure if the query was really executed or was retrieved from the query cache (it can be a big difference).
Upvotes: 0
Reputation: 717
You could use microtime(true) to get the seconds needed to execute:
$start = microtime(true);
//Your code
echo 'Time needed: ' . (microtime(true) - $start) . '.';
To get a more accurate time you should do that test more times (10000 here):
$start = microtime(true);
for ($i = 0; $i < 10000; ++$i) {
//Your code
}
echo 'Time needed: ' . (microtime(true) - $start) . '.';
@Baz1nga, that code doesn't work, since microtime_float() isn't a function (Sorry, I can't comment, need more reputation :()
Upvotes: 6