Last active
July 4, 2020 14:08
-
-
Save ifknot/abd7987f1488e15045f35d33554102cb to your computer and use it in GitHub Desktop.
stopwatch iodiom high precision timer for measuring software performance
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
/** | |
* @brief stopwatch idiom high precision timer for measuring software performance | |
* @tparam PolicyClock - std::chrono::steady_clock (default), system_clock, high_resolution_clock | |
*/ | |
template<typename PolicyClock = std::chrono::steady_clock> | |
class stopwatch { | |
using clock_t = PolicyClock; | |
public: | |
using time_point_t = typename clock_t::time_point; | |
stopwatch() = default; | |
time_point_t static now() { | |
return clock_t::now(); | |
} | |
time_point_t start() { | |
before = clock_t::now(); | |
return before; | |
} | |
time_point_t stop() { | |
after = clock_t::now(); | |
return after; | |
} | |
template<typename duration_t> | |
uint64_t count() { | |
return std::chrono::duration_cast<duration_t>(after - before).count(); | |
} | |
float seconds() { | |
std::chrono::duration<float> t = after - before; | |
return t.count(); | |
} | |
private: | |
time_point_t before; | |
time_point_t after; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment