district5/timer

District5 计时器库

1.0.0 2022-10-19 07:54 UTC

This package is auto-updated.

Last update: 2024-09-19 12:35:09 UTC


README

关于

此库支持计时器功能。

安装

此库不需要其他库。

composer require district5/timer

运行单元测试

$ composer install --dev
$ ./vendor/bin/phpunit

用法

秒表

秒表类可以实例化并用于测量从秒表开始的时间间隔

$stopwatch = new \District5\Timer\Stopwatch();
$stopwatch->start();

...

$secondsPassed = $stopwatch->secondsPassed();

您还可以设置最大时间,以便进行布尔检查以查看是否已超过该时间。这在处理具有最大执行时间的cron任务时非常有用。

$maxTimeSeconds = 300;

$stopwatch = new \District5\Timer\Stopwatch($maxTimeSeconds);
$stopwatch->start();

while (!$stopwatch->hasMaxTimePassed())
{
    // do some work
}

在上面的示例中,您应考虑一个项目在while循环中处理迭代所需的时间,以将允许的最大秒数设置为小于“cron可以运行的最大时间” - “处理1次循环迭代所需的时间”。

问题

记录一个错误报告!