Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
While doing Drupal development, you sometimes might want to find out how fast or slow a certain part in your code is. Did you know that Drupal has 3 easy functions to do this built right in to core, timer_start($name)
, timer_read($name)
and timer_stop($name)
.
You can have multiple timers in one run and each one of them is identified by the $name
parameter you have to pass to each of these three functions.
I think the functions are pretty self explanatory:
timer_start
starts a timertimer_read
tells you how long the timer has been running- and
timer_stop
stops the timer and returns an array that contains the number of times the timer has been started and stopped (count) and the accumulated timer value in ms (time).
Drupal 8
The Drupal 8 equivalent for these functions can be found in the Drupal\Component\Utility\Timer
class.
Timer::start
starts a timerTimer::read
tells you how long the timer has been running- and
Timer::stop
stops the timer and returns an array that contains the number of times the timer has been started and stopped (count) and the accumulated timer value in ms (time).