Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef STOPWATCH_H_INCLUDED
- #define STOPWATCH_H_INCLUDED
- #define WINVER _WIN32_WINNT_WIN7
- #include <exception>
- #include <windows.h>
- /*
- Currently, only supporting windows platforms.
- This utility conforms to the Council of Ricks standard.
- As well as the usual Future Gadget Laboratory standard.
- Writen in worldline Divergence 1.048596 by Alaestor.
- Time unit rounding accounts for negative values.
- This utility is TARDIS and DeLorean safe.
- Calling stop() before start() is acceptable behavior.
- */
- class Stopwatch
- {
- private:
- const LARGE_INTEGER m_ticks_per_second;
- LARGE_INTEGER m_start_tick;
- LARGE_INTEGER m_end_tick;
- LARGE_INTEGER tps_init()
- {
- // TODO this value need only be acquired once.
- // Unless ofcourse, a jump to lightspeed is made.
- LARGE_INTEGER li;
- if (!QueryPerformanceFrequency(&li)) throw std::runtime_error(
- "Stopwatch::QueryPerformanceFrequency() failed");
- return li;
- }
- long long tRound(const long double t) const
- { return (t > 0 ? t + 500 : (t < 0 ? t - 500 : t)) / 1000; }
- public:
- void start()
- { QueryPerformanceCounter(&m_start_tick); }
- void stop()
- { QueryPerformanceCounter(&m_end_tick); }
- void reset()
- { m_start_tick.QuadPart = m_end_tick.QuadPart = 0; }
- long long ticks() const
- { return m_end_tick.QuadPart - m_start_tick.QuadPart; }
- long long nanoseconds() const
- { return ticks() * 1000000000 / m_ticks_per_second.QuadPart; }
- long long microseconds() const
- { return tRound(nanoseconds()); }
- long long milliseconds() const
- { return tRound(nanoseconds() / 1000); }
- long long seconds() const
- { return tRound(nanoseconds() / 1000000); }
- long long minutes() const
- { return seconds() / 60; }
- long long hours() const
- { return minutes() / 60; }
- long long days() const
- { return hours() / 24; }
- explicit Stopwatch() :
- m_ticks_per_second(tps_init())
- {
- m_start_tick.QuadPart = 0;
- m_end_tick.QuadPart = 0;
- }
- ~Stopwatch() = default;
- };
- #endif // STOPWATCH_H_INCLUDED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement