Friday, November 11, 2016

ScopedStopWatch

ScopedStopWatch

Hello guys,

As suggested by carl in my previous post (StopWatch) This is an extension to the earlier StopWatch class.
This class is called ScopedStopWatch. Basically while you are profiling any method you mark the start and end of the method and time taken by that method. ScopedStopWatch class does this for you.

ScopedStopWatch(const std::string & method_name, std::ostream & os = std::cout);

You have to instantiate ScopedStopWatch with two parameters. first would be the function name ( you have to pass that as std::string) second would be the std::ostream to which you need to output the logs (default is std::cout). Thats all, the log statments would be printed to the given std::ostream automatically by ScopedStopWatch class.


void some_method_scoped_stop_watch_example()
{
techtips::ScopedStopWatch scopedStopWatch("some_method_scoped_stop_watch_example", std::cout);

// Do some processing
std::cout << "I am printing 10 lines of logs." << std::endl;
for(int i=0;i<10;i++)
{
std::cout << "This is my log " << i << std::endl;
}
}


You can find the ScopedStopWatch class implementation with working example here.

Go to link Download