2011-07-04 196 views
-1

我想看看我的网站爬上需要多长时间。PHP速度测试代码

我试过这段代码:

$timer = explode(' ', microtime()); 
    $timer = $timer[1] + $timer[0]; 
    print $timer; 

但它表明:

1309741766.46

  • 注:而每两秒钟就上升。

例如:更多个秒钟,这两点是:

1309741767.46

而且更多个秒时:

1309741768.46

我会感激帮助。

+1

看一看例子在'microtime'曼努埃尔页面http:// PHP。 net/manual/en/function.microtime.php –

+1

欲了解* what *的帮助吗?毫无疑问。 PS:'microtime()'有*有用*参数;-) – zerkms

+0

@stealthyninja - 你应该很好地意识到攀登。喜欢躲在树林和屋顶上。我经常有忍者页面,非常善于爬树,躲在树叶里。 – evan

回答

2

我不明白你说的话,但是......

$start = microtime(true); 
echo microtime(true) - $start; 
+0

它打印出来: 9.53674316406E-7 它好吗? – Daniel

2

microtime返回包含微秒,秒的字符串。你正在总结微秒和秒,这会导致奇怪的结果。

microtime还包括一个get_as_float参数,它可能是你想要的。

-2

这里是如何衡量以秒为:

$page_rendering_start_time = microtime_float(); 
... 
... 
... 
$current_microtime = microtime_float(); 
$page_rendering_time_seconds = sprintf("%.4f", $current_microtime - $$page_rendering_start_time); 

echo "Page rendering time: " . $page_rendering_time_seconds . " seconds"; 

function microtime_float() 
{ 
    list($msec, $sec) = explode(' ', microtime()); 
    $microtime = (float)$msec + (float)$sec; 

    return $microtime; 
} 

它打印“页面渲染时间:0.0034秒”

+0

应该指出的是,这只适用于PHP 4,因为PHP 5引入了'get_as_float'参数,正如Yann和webarto的答案中所解释的那样。 – lonesomeday

+0

2 lonesomeday:我的代码在任何PHP中都能很好地工作,那里绝对没有必要给我-1! –

+2

我没有 - 我同意,它适用于任何PHP版本,但在PHP 5中毫无意义(并且速度很慢)。这是PHP 4中最好的解决方案。 – lonesomeday