Skip to content

Conversation

@tlively
Copy link
Member

@tlively tlively commented Jan 8, 2026

Reduce verbosity by implicitly starting the timer when it is first
constructed. Separate concerns by removing printing functionality.
Add the ability to measure time elapsed since the last measurement to
better support the use case of measuring several subsequent steps of an
operation as well as the total elapsed time.

Reduce verbosity by implicitly starting the timer when it is first
constructed. Separate concerns by removing printing functionality.
Add the ability to measure time elapsed since the last measurement to
better support the use case of measuring several subsequent steps of an
operation as well as the total elapsed time.
@tlively tlively requested a review from kripken January 8, 2026 22:35

public:
Timer(std::string name = "") : name(name) {}
Timer() { restart(); }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes the following pattern I use more difficult:

static Timer timer("work");

int foo() {
  ..
  timer.start(); ... timer.end();
  ..
}

int bar() {
  ..
  timer.start(); ... timer.end();
  ..
}

To collect timings from multiple functions (or multiple invocations of the same function), it seems better to manually start?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case it's trivial to just have a different timer for each function.

In DAE2 I currently have lines that look like this just to measure parts of a larger operation:

TIME(timer.stop(); timer.dump(); timer = Timer("rewrite"); timer.start());

With this change, this just becomes this:

TIME(std::cerr << "some time: " << timer.lastElapsed() << "\n");

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case it's trivial to just have a different timer for each function.

But imagine that the timer measures a certain type of shared work. E.g. it's nice to see the total validation time across all functions, or the total LocalGraph time over multiple invocations of the same function (each of which might be very fast).

How about adding a RunningTimer for what you want?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, the global state can be the time and it can be updated locally by doing Timer localTimer; ... globalTime += localTimer.totalElapsed(). This is the same number of lines necessary for that use case in the current API, but plays better with multithreading. I don't think it's worth having multiple timer utilities when there is only a single usage checked in.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough.

@tlively tlively merged commit e1dc453 into main Jan 9, 2026
17 checks passed
@tlively tlively deleted the timer-refactor branch January 9, 2026 00:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants