Skip to content

Instantly share code, notes, and snippets.

@fbaeuerlein
Created March 12, 2014 09:36
Show Gist options
  • Save fbaeuerlein/9503708 to your computer and use it in GitHub Desktop.
Save fbaeuerlein/9503708 to your computer and use it in GitHub Desktop.
scope guard like class for measuring duration (from construction to destruction)
#include <boost/chrono/chrono_io.hpp>
class MeasureDuration
{
public:
MeasureDuration(const std::string & name = "unknown" )
{
_duration = 0;
_name = name;
_start = boost::chrono::high_resolution_clock::now();
}
~MeasureDuration()
{
boost::chrono::nanoseconds ns = boost::chrono::high_resolution_clock::now() - _start;
_duration = (double) ns.count() / (1000. * 1000.);
std::cerr << "duration of " << _name << " in ms: " << _duration << std::endl;
}
private:
double _duration;
std::string _name;
boost::chrono::high_resolution_clock::time_point _start;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment