-
-
Save fabiogaluppo/3c76023a5da2a2c92dd3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <chrono> | |
struct stop_watch final | |
{ | |
stop_watch() : Start_(now()) {} | |
std::chrono::seconds elapsed_s() const | |
{ | |
using std::chrono::seconds; | |
return std::chrono::duration_cast<seconds>(elapsed()); | |
} | |
std::chrono::milliseconds elapsed_ms() const | |
{ | |
using std::chrono::milliseconds; | |
return std::chrono::duration_cast<milliseconds>(elapsed()); | |
} | |
std::chrono::microseconds elapsed_us() const | |
{ | |
using std::chrono::microseconds; | |
return std::chrono::duration_cast<microseconds>(elapsed()); | |
} | |
std::chrono::nanoseconds elapsed_ns() const | |
{ | |
using std::chrono::nanoseconds; | |
return std::chrono::duration_cast<nanoseconds>(elapsed()); | |
} | |
void restart() { Start_ = now(); } | |
stop_watch(const stop_watch&) = delete; | |
stop_watch& operator=(const stop_watch&) = delete; | |
private: | |
static std::chrono::high_resolution_clock::time_point now() | |
{ | |
return std::chrono::high_resolution_clock::now(); | |
} | |
std::chrono::duration<double> elapsed() const | |
{ | |
return now() - Start_; | |
} | |
std::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