Skip to content

Instantly share code, notes, and snippets.

@luluco250
Created June 5, 2018 21:19
Show Gist options
  • Save luluco250/92dd79522447f64eb51e740331b0d082 to your computer and use it in GitHub Desktop.
Save luluco250/92dd79522447f64eb51e740331b0d082 to your computer and use it in GitHub Desktop.
C++ Stopwatch
#ifndef STOPWATCH_HPP
#define STOPWATCH_HPP
#include <chrono>
namespace detail {
using namespace std::chrono;
using hrc = high_resolution_clock;
class Stopwatch {
protected:
hrc::time_point _start, _last_lap;
public:
void start() {
_start = hrc::now();
_last_lap = _start;
}
float lap() {
auto d = duration<float>(hrc::now() - _last_lap);
_last_lap = hrc::now();
return d.count();
}
float reset() {
auto d = duration<float>(hrc::now() - _start);
start();
return d.count();
}
};
}
using Stopwatch = detail::Stopwatch;
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment