[NFC] Update the timer utility#8187
Conversation
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.
|
|
||
| public: | ||
| Timer(std::string name = "") : name(name) {} | ||
| Timer() { restart(); } |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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");
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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.