Skip to content

Instantly share code, notes, and snippets.

@oliora
Created September 26, 2016 08:14
Show Gist options
  • Save oliora/8c6f5b46d0e6121b593e3477c50ced37 to your computer and use it in GitHub Desktop.
Save oliora/8c6f5b46d0e6121b593e3477c50ced37 to your computer and use it in GitHub Desktop.
Performance timer in pure C++11
#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