Add Timer::reset() method

Created on 26 March 2015, almost 10 years ago
Updated 20 February 2023, almost 2 years ago

Problem/Motivation

The Timer utility is missing what seems like a fundamental concept: the ability to reset a timer. Without such an ability, a single timer can't be used inside a loop, for example, without some kind of awkward runtime timer name randomization. (I discovered this trying to graph the amount of time it takes to search index nodes in large batches.)

Before:

Timer::start('test');
usleep(5000);
$value1 = Timer::read('test');
// ~5
Timer::stop('test');
Timer::start('test');
usleep(5000);
$value2 = Timer::read('test');
// ~10


Timer::start('test2');
usleep(5000);
$value1 = Timer::read('test2');
// ~5
Timer::start('test2');
usleep(5000);
$value2 = Timer::read('test2');
// ~5

After:

Timer::start('test');
usleep(5000);
$value1 = Timer::read('test');
// ~5
Timer::stop('test');
Timer::start('test', TRUE);
usleep(5000);
$value2 = Timer::read('test');
// ~5


Timer::start('test2');
usleep(5000);
$value1 = Timer::read('test2');
// ~5
Timer::stop('test2');
Timer::reset('test2');
Timer::start('test2');
usleep(5000);
$value2 = Timer::read('test2');
// ~5

Proposed resolution

Add a Timer::reset() method.

Remaining tasks

  • Review.

User interface changes

None.

API changes

New method.

Feature request
Status

Needs work

Version

10.1

Component
Base 

Last updated about 11 hours ago

Created by

🇺🇸United States traviscarden

Live updates comments and jobs are added and updated live.
  • Quick fix

    Very small and simple change. Preferred over Quickfix.

  • Needs documentation

    A documentation change is requested elsewhere. For Drupal core (and possibly other projects), once the change has been committed, this status should be recorded in a change record node.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇬🇧United Kingdom catch

    The example in the issue summary doesn't match the new API, it's still using the $reset method parameter. I think the new method could use extra documentation, even if it's 'Resets the current timer to 0', should also have a :void return type hint now.

    @zaporylie's point in #6 looks relevant too - we can deprecate methods now, so should we open a follow-up to look at adding Timer::pause() and redefining Timer::stop()?

Production build 0.71.5 2024