Feature: Add StopWatch and ScopedStopWatch
- Add
StopWatch
: Will start on construction or on callingreset()
, and will stop and return the duration when callingstop()
. Does use system time (TimeMode::SystemTime
) by default, but can also work with virtual time (TimeMode::VirtualTime
). Also has a static methodassess()
, which takes a lambda, and returns the time it took to execute the lambda. - Add
ScopedStopWatch
: Takes a lambda as construction parameter (taking anIceUtil::Time
as parameter), which will be called at destruction (i.e., when the scope was left). - Introduce enum for time mode (
TimeMode
) to replace boolean flags
Code examples:
StopWatch
:
// By supplied methods.
StopWatch sw;
long_operation();
IceUtil::Time duration = sw.stop();
std::cout << "Operation took " << duration.toMilliSeconds() << " ms";
// By executing a lambda.
IceUtil::Time duration = StopWatch::assess([&]() {
long_operation();
});
std::cout << "Operation took " << duration.toMilliSeconds() << " ms";
ScopedStopWatch
:
// By scope existance.
{
ScopedStopWatch sw{[](IceUtil::Time duration) {
std::cout << "Operation took " << duration.toMilliSeconds() << " ms";
}};
long_operation();
}
Edited by Christian Dreher