Skip to content

Instantly share code, notes, and snippets.

@zhpengg
Forked from radiospiel/stop_watch.inl
Created March 17, 2012 12:46
Show Gist options
  • Save zhpengg/2058530 to your computer and use it in GitHub Desktop.
Save zhpengg/2058530 to your computer and use it in GitHub Desktop.
#include <sys/time.h> // for gettimeofday()
class StopWatch {
timeval started;
std::string msg;
public:
StopWatch(const std::string& m): msg(m)
{ gettimeofday(&started, NULL); }
~StopWatch() {
std::cerr << msg << " " << usecs() << " µsecs" << std::endl;
}
unsigned usecs() const {
timeval t2;
gettimeofday(&t2, NULL);
return (t2.tv_sec - started.tv_sec) * 1000000 + (t2.tv_usec - started.tv_usec);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment