Measuring the time of a query
I'm using PDO, is there any way, to measure how long does it 开发者_开发百科take to complete the query?
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 :()
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);
}
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).
精彩评论