mengshaoying / timer
计算时间差
1.0.0
2023-01-17 09:10 UTC
Requires
- php: ^5.3
This package is auto-updated.
Last update: 2024-09-17 12:59:12 UTC
README
关于仓库中的代码
编写程序时,可能会想要计算两个时间点之间代码执行的时间,这时就需要计算时间差了。在PHP中,可以使用hrtime
(需要PHP版本大于或等于7.3.0
)或microtime
函数来获取当前精确的时间,从而能够计算短时间内的时间间隔。这个仓库中有一个Timer
类,就是为了实现这种需求而设计的,它会根据运行环境自动使用hrtime
或microtime
来记录时间。
使用说明
通过实例化Timer
类来获取当前的时间,然后调用对象方法来获取两个时间对象的时间差。示例:
$t0 = new Timer\Timer(); for ($i = 0; $i < 10000000; $i++) { $x = mt_rand(); } $t1 = new Timer\Timer(); // 输出时间差,t1 - t0 echo $t1->sub($t0); echo PHP_EOL;
示例代码输出类似:
0.772363515
可以通过运行仓库中的php index.php
来查看示例效果。