Skip to content

Feature: Add StopWatch and ScopedStopWatch

Christian Dreher requested to merge feature/time-assessing into master
  • Add StopWatch: Will start on construction or on calling reset(), and will stop and return the duration when calling stop(). Does use system time (TimeMode::SystemTime) by default, but can also work with virtual time (TimeMode::VirtualTime). Also has a static method assess(), which takes a lambda, and returns the time it took to execute the lambda.
  • Add ScopedStopWatch: Takes a lambda as construction parameter (taking an IceUtil::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

Merge request reports