2011-10-09 42 views
1

我正在使用PDO来测量完成查询需要多长时间?测量查询的时间

+0

请告诉我们您正在使用哪个数据库。 –

+0

哎呀对,我假设MySQL,我们需要知道它是否真的是MySQL! – stivlo

回答

5

你可以使用microtime中(真)去执行所需的秒:

$start = microtime(true); 
//Your code 
echo 'Time needed: ' . (microtime(true) - $start) . '.'; 

为了得到更准确的时间,你应该做的测试多次(10000这里):

$start = microtime(true); 
for ($i = 0; $i < 10000; ++$i) { 
    //Your code 
} 
echo 'Time needed: ' . (microtime(true) - $start) . '.'; 

@ Baz1nga,该代码不起作用,因为microtime_float()不是一个函数(对不起,我不能评论,需要更多的声誉:()

1

您是否尝试过使用Micro 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); 
} 
+0

请看@Sietse的回答,他正在和你说话。 – Rym

0

正如Sietse所说,使用microtime函数来测量经过的微秒。

如果你测试查询之前测试的MySQL,执行以下查询:

RESET QUERY CACHE; 

否则,如果该查询是真正执行还是从查询缓存被检索你不能肯定(它可以是一个很大的区别)。