Created
September 26, 2016 08:14
-
-
Save oliora/8c6f5b46d0e6121b593e3477c50ced37 to your computer and use it in GitHub Desktop.
Performance timer in pure C++11
This file contains hidden or 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> | |
#include <type_traits> | |
// Floating seconds duration | |
using fseconds = std::chrono::duration<double>; | |
class PerfTimer | |
{ | |
public: | |
using clock = std::conditional< | |
std::chrono::high_resolution_clock::is_steady, | |
std::chrono::high_resolution_clock, | |
std::chrono::steady_clock | |
>::type; | |
PerfTimer(): m_start(clock::now()) {} | |
fseconds passed() const { return clock::now() - m_start; } | |
private: | |
clock::time_point m_start; | |
}; | |
// Usage example | |
void test() | |
{ | |
PerfTimer timer; | |
... // Time measured piece of code | |
auto time_spent = timer.passed(); | |
std::cout << "Time spent, seconds: " << time_spent.count(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment