测量php脚本执行时间的准确方法 [英] Accurate way to measure execution times of php scripts

查看:67
本文介绍了测量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;

这篇关于测量php脚本执行时间的准确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆