启动和停止计时器PHP

时间:2011-11-29 12:12:42

标签: php timer

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

由于

8 个答案:

答案 0 :(得分:98)

您可以使用microtime并计算差异:

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

以下是microtimehttp://php.net/manual/en/function.microtime.php

的PHP文档

答案 1 :(得分:7)

使用microtime功能。该文档包含示例代码。

答案 2 :(得分: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 :(得分:3)

您可以使用Timer Class

    <?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;
   }
}

?>

答案 4 :(得分:2)

从PHP 7.3开始,hrtime函数应在任何时间使用。

$start = hrtime(true); // Nanoseconds
// execute...
$end = hrtime(true);   // Nanoseconds

echo ($end - $start) / 1000000000; // Seconds

提到的microtime函数依赖于系统时钟。可以修改例如通过ubuntu上的ntpd程序或仅是sysadmin。

答案 5 :(得分: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"

答案 6 :(得分:0)

您还可以使用HRTime软件包。它具有类StopWatch。

答案 7 :(得分:0)

或者,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()允许清理和组织多次调用,而处理特殊情况,例如重叠