启动和停止一个 PHP 计时器

73

我需要关于在PHP中启动和停止计时器的信息。我需要测量从开始运行我的.exe程序(在我的PHP脚本中使用exec()函数)到完成执行所花费的时间,并以秒为单位显示所花费的时间。

我应该如何实现?

8个回答

137

您可以使用microtime函数来计算时间差:

$time_pre = microtime(true);
exec(...);
$time_post = microtime(true);
$exec_time = $time_post - $time_pre;

这里是microtime的PHP文档:http://php.net/manual/en/function.microtime.php


16

自从PHP 7.3版本,hrtime函数应该被用于仪器化。

$start = hrtime(true);
// run your code...
$end = hrtime(true);   

echo ($end - $start);                // Nanoseconds
echo ($end - $start) / 1000000;      // Milliseconds
echo ($end - $start) / 1000000000;   // Seconds

所提到的microtime函数依赖于系统时钟,该时钟可以被修改,例如在ubuntu上使用ntpd程序或由系统管理员进行修改。


8
使用microtime函数。官方文档中包含样例代码。

6

针对你的需求,下面这个简单的类应该就可以满足你:

class Timer {
    private $time = null;
    public function __construct() {
        $this->time = time();
        echo 'Working - please wait..<br/>';
    }

    public function __destruct() {
        echo '<br/>Job finished in '.(time()-$this->time).' seconds.';
    }
}


$t = new Timer(); // echoes "Working, please wait.."

[some operations]

unset($t);  // echoes "Job finished in n seconds." n = seconds elapsed

3
你可以使用计时器类。
    <?php

class Timer {

   var $classname = "Timer";
   var $start     = 0;
   var $stop      = 0;
   var $elapsed   = 0;

   # Constructor
   function Timer( $start = true ) {
      if ( $start )
         $this->start();
   }

   # Start counting time
   function start() {
      $this->start = $this->_gettime();
   }

   # Stop counting time
   function stop() {
      $this->stop    = $this->_gettime();
      $this->elapsed = $this->_compute();
   }
   
   # Get Elapsed Time
   function elapsed() {
      if ( ! $this->elapsed )
         $this->stop();

      return $this->elapsed;
   }
   
   # Resets Timer so it can be used again
   function reset() {
      $this->start   = 0;
      $this->stop    = 0;
      $this->elapsed = 0;
   }

   #### PRIVATE METHODS ####
   
   # Get Current Time
   function _gettime() {
      $mtime = microtime();
      $mtime = explode( " ", $mtime );
      return $mtime[1] + $mtime[0];
   }
   
   # Compute elapsed time
   function _compute() {
      return $this->stop - $this->start;
   }
}

?>

1
此外,您还可以使用HRTime包。它有一个名为StopWatch的类。

0
class Timer
{
    private $startTime = null;

    public function __construct($showSeconds = true)
    {
        $this->startTime = microtime(true);
        echo 'Working - please wait...' . PHP_EOL;
    }

    public function __destruct()
    {
        $endTime = microtime(true);
        $time = $endTime - $this->startTime;

        $hours = (int)($time / 60 / 60);
        $minutes = (int)($time / 60) - $hours * 60;
        $seconds = (int)$time - $hours * 60 * 60 - $minutes * 60;
        $timeShow = ($hours == 0 ? "00" : $hours) . ":" . ($minutes == 0 ? "00" : ($minutes < 10 ? "0" . $minutes : $minutes)) . ":" . ($seconds == 0 ? "00" : ($seconds < 10 ? "0" . $seconds : $seconds));

        echo 'Job finished in ' . $timeShow . PHP_EOL;
    }
}

$t = new Timer(); // echoes "Working, please wait.."

[some operations]

unset($t);  // echoes "Job finished in h:m:s"

0
作为替代方案,PHP具有内置的计时器控制器:new EvTimer()
它可以用于制作任务调度程序,并正确处理特殊情况。
这不仅是时间,而且还是一个时间传输层、计时器、圈数计数器,就像一个带有PHP回调的秒表 ;)
EvTimer观察器是简单的相对定时器,它们在一段时间后生成事件,并在之后的规则间隔中可选择重复执行。这些定时器基于实际时间,也就是说,如果注册一个事件,在一个小时后超时并将系统时钟重置为去年1月,那么它仍然在一个小时后超时。
回调保证只有在超时后才被调用(...). 如果多个定时器在同一次循环迭代期间准备好,则具有较早超时值的定时器会在具有相同优先级但较晚超时值的定时器之前被调用。
定时器本身将尽力避免漂移,也就是说,如果将计时器配置为每10秒触发一次,则通常会准确地触发每10秒一次。但是,如果脚本无法跟上计时器,因为它需要比这10秒更长的时间,则计时器不会在每个事件循环迭代中触发多次。
第一个和第二个参数允许控制执行前的时间延迟和迭代次数。
第三个参数是在每次迭代中调用的回调函数。
after

    Configures the timer to trigger after after seconds.

repeat

    If repeat is 0.0 , then it will automatically be stopped once the timeout is reached.
    If it is positive, then the timer will automatically be configured to trigger again every repeat seconds later, until stopped manually.

https://www.php.net/manual/en/class.evtimer.php

https://www.php.net/manual/en/evtimer.construct.php

$w2 = new EvTimer(2, 1, function ($w) {
    echo "is called every second, is launched after 2 seconds\n";
    echo "iteration = ", Ev::iteration(), PHP_EOL;

    // Stop the watcher after 5 iterations
    Ev::iteration() == 5 and $w->stop();
    // Stop the watcher if further calls cause more than 10 iterations
    Ev::iteration() >= 10 and $w->stop();
});

当然,我们可以使用基本循环和一些时间函数(例如sleep()usleep()hrtime())轻松创建此功能,但是new EvTimer()可以在处理重叠等特殊情况时进行清洁和组织的多次调用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接