class Timer
Same name in other branches
- 8.9.x core/lib/Drupal/Component/Utility/Timer.php \Drupal\Component\Utility\Timer
- 10 core/lib/Drupal/Component/Utility/Timer.php \Drupal\Component\Utility\Timer
- 11.x core/lib/Drupal/Component/Utility/Timer.php \Drupal\Component\Utility\Timer
Provides helpers to use timers throughout a request.
Hierarchy
- class \Drupal\Component\Utility\Timer
Expanded class hierarchy of Timer
Related topics
5 files declare their use of Timer
- batch.inc in core/
includes/ batch.inc - Batch processing API for processes to run in multiple HTTP requests.
- Cron.php in core/
lib/ Drupal/ Core/ Cron.php - JSWebAssertTest.php in core/
tests/ Drupal/ FunctionalJavascriptTests/ Tests/ JSWebAssertTest.php - TimerTest.php in core/
tests/ Drupal/ Tests/ Component/ Utility/ TimerTest.php - ViewUI.php in core/
modules/ views_ui/ src/ ViewUI.php
File
-
core/
lib/ Drupal/ Component/ Utility/ Timer.php, line 10
Namespace
Drupal\Component\UtilityView source
class Timer {
protected static $timers = [];
/**
* Starts the timer with the specified name.
*
* If you start and stop the same timer multiple times, the measured intervals
* will be accumulated.
*
* @param $name
* The name of the timer.
*/
public static function start($name) {
static::$timers[$name]['start'] = microtime(TRUE);
static::$timers[$name]['count'] = isset(static::$timers[$name]['count']) ? ++static::$timers[$name]['count'] : 1;
}
/**
* Reads the current timer value without stopping the timer.
*
* @param string $name
* The name of the timer.
*
* @return int
* The current timer value in ms.
*/
public static function read($name) {
if (isset(static::$timers[$name]['start'])) {
$stop = microtime(TRUE);
$diff = round(($stop - static::$timers[$name]['start']) * 1000, 2);
if (isset(static::$timers[$name]['time'])) {
$diff += static::$timers[$name]['time'];
}
return $diff;
}
return static::$timers[$name]['time'];
}
/**
* Stops the timer with the specified name.
*
* @param string $name
* The name of the timer.
*
* @return array
* A timer array. The array contains the number of times the timer has been
* started and stopped (count) and the accumulated timer value in ms (time).
*/
public static function stop($name) {
if (isset(static::$timers[$name]['start'])) {
$stop = microtime(TRUE);
$diff = round(($stop - static::$timers[$name]['start']) * 1000, 2);
if (isset(static::$timers[$name]['time'])) {
static::$timers[$name]['time'] += $diff;
}
else {
static::$timers[$name]['time'] = $diff;
}
unset(static::$timers[$name]['start']);
}
return static::$timers[$name];
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
Timer::$timers | protected static | property | |
Timer::read | public static | function | Reads the current timer value without stopping the timer. |
Timer::start | public static | function | Starts the timer with the specified name. |
Timer::stop | public static | function | Stops the timer with the specified name. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.