【问题标题】:Start and stop a timer PHP启动和停止计时器 PHP
【发布时间】:2011-11-29 12:12:41
【问题描述】:

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

我该怎么做?

【问题讨论】:

    标签: php timer


    【解决方案1】:

    自 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 程序或只是系统管理员。

    【讨论】:

      【解决方案2】:

      您可以使用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

      【讨论】:

        【解决方案3】:

        使用microtime 函数。文档包括示例代码。

        【讨论】:

          【解决方案4】:

          您也可以使用HRTime 包。它有一个类 StopWatch。

          【讨论】:

            【解决方案5】:

            出于您的目的,这个简单的类应该就是您所需要的:

            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
            

            【讨论】:

              【解决方案6】:

              你可以使用定时器类

                  <?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 ( !$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;
                 }
              }
              
              ?>
              

              【讨论】:

                【解决方案7】:
                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"
                

                【讨论】:

                  【解决方案8】:

                  作为替代方案,php 有一个内置的计时器控制器:new EvTimer()

                  它可以用来做一个任务调度器,对特殊情况有适当的处理。

                  这不仅是时间,而且是时间传输层、计时器、计圈器,就像秒表一样,但带有 php 回调;)

                  EvTimer 观察者是生成事件的简单相对计时器 在给定时间后,并可选择定期重复 之后。

                  计时器基于实时,也就是说,如果一个人注册了一个事件 一小时后超时并将系统时钟重置为一月 去年,它仍然会在(大约)一小时后超时。

                  保证只有在超时后才会调用回调 通过(...)。如果多个计时器在 相同的循环迭代然后具有 较早超时值的那些是 在具有相同优先级的优先级之前调用,超时值较晚。

                  计时器本身将尽最大努力避免漂移,也就是说,如果 定时器被配置为每 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()允许清理和有组织的多次调用,同时处理像重叠这样的特殊情况

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2023-01-31
                    • 1970-01-01
                    相关资源
                    最近更新 更多