sy / stopwatch
计时实用类
1.0.0
2020-10-02 10:54 UTC
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/phpunit: ^9
This package is auto-updated.
Last update: 2024-09-11 01:03:05 UTC
README
计时实用类
安装
使用以下命令安装最新版本
$ composer require sy/stopwatch
基本用法
<?php use Sy\Stopwatch; $stopwatch = new Stopwatch(); // Start timing $stopwatch->start(); // Process to be timed for ($i = 0; $i < 100000; $i++) { // Do something... } // Stop timing $stopwatch->stop(); // Retrieve time in seconds $time = $stopwatch->getTime(); echo "Execution time: $time seconds";
高级用法
您可以为每个计时器添加一个特定的 标签。
在 停止 后,您可以再次 启动 计时器,时间将添加到计时器。否则,您必须 重置 计时器。
示例
<?php use Sy\Stopwatch; $stopwatch = new Stopwatch(); for ($i = 0; $i < 10; $i++) { // Start timing process A $stopwatch->start('A'); // Process A for ($a = 0; $a < 1000; $a++) { // Do something... } // Stop timing process A $stopwatch->stop('A'); // Start timing process B $stopwatch->start('B'); for ($b = 0; $b < 1000; $b++) { // Do something... } // Stop timing process B $stopwatch->stop('B'); } // Retrieve time $timeA = $stopwatch->getTime('A'); $timeB = $stopwatch->getTime('B');