测量php脚本执行时间的准确方法

测量php脚本执行时间的准确方法

问题描述:

我想知道PHP for循环执行需要多少毫秒.

I want to know how many milliseconds a PHP for-loop takes to execute.

我知道通用算法的结构,但不知道如何在PHP中实现它:

I know the structure of a generic algorithm, but no idea how to implement it in PHP:

Begin
init1 = timer(); // where timer() is the amount of milliseconds from midnight
the loop begin
some code
the loop end
total = timer() - init1;
End

您可以使用 microtime 函数为了这.从文档:

microtime —返回当前的Unix时间戳(以微秒为单位)

microtime — Return current Unix timestamp with microseconds

如果get_as_float设置为TRUE,则microtime()返回一个浮点数,它表示自Unix纪元精确到最接近的微秒以来的当前时间(以秒为单位).

If get_as_float is set to TRUE, then microtime() returns a float, which represents the current time in seconds since the Unix epoch accurate to the nearest microsecond.

示例用法:

$start = microtime(true);
while (...) {

}
$time_elapsed_secs = microtime(true) - $start;