Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   1  <?php
   2  /**
   3   * Simple interface for timing operations.
   4   *
   5   * <code>
   6   *  $t = new Horde_Support_Timer;
   7   *  $t->push();
   8   *  $elapsed = $t->pop();
   9   * </code>
  10   *
  11   * Copyright 1999-2017 Horde LLC (http://www.horde.org/)
  12   *
  13   * @category   Horde
  14   * @package    Support
  15   * @license    http://www.horde.org/licenses/bsd
  16   */
  17  class Horde_Support_Timer
  18  {
  19      /**
  20       * Holds the starting timestamp.
  21       *
  22       * @var array
  23       */
  24      protected $_start = array();
  25  
  26      /**
  27       * Current index for stacked timers.
  28       *
  29       * @var integer
  30       */
  31      protected $_idx = 0;
  32  
  33      /**
  34       * Push a new timer start on the stack.
  35       */
  36      public function push()
  37      {
  38          $start = $this->_start[$this->_idx++] = microtime(true);
  39          return $start;
  40      }
  41  
  42      /**
  43       * Pop the latest timer start and return the difference with the current
  44       * time.
  45       *
  46       * @return float The amount of time passed.
  47       */
  48      public function pop()
  49      {
  50          $etime = microtime(true);
  51  
  52          if (! ($this->_idx > 0)) {
  53              throw new Exception('No timers have been started');
  54          }
  55  
  56          return $etime - $this->_start[--$this->_idx];
  57      }
  58  
  59  }