Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Billy provided a great answer based on the ISO C++ standard that I fully agree with. However there is another side of the story - real life. It seems that right now there is really no difference between those clocks in implementation of popular compilers:</p> <p>gcc 4.8:</p> <pre><code>#ifdef _GLIBCXX_USE_CLOCK_MONOTONIC ... #else typedef system_clock steady_clock; #endif typedef system_clock high_resolution_clock; </code></pre> <p>Visual Studio 2012:</p> <pre><code>class steady_clock : public system_clock { // wraps monotonic clock public: static const bool is_monotonic = true; // retained static const bool is_steady = true; }; typedef system_clock high_resolution_clock; </code></pre> <p>In case of gcc you can check if you deal with steady clock simply by checking <code>is_steady</code> and behave accordingly. However VS2012 seems to cheat a bit here :-)</p> <p>If you need high precision clock I recommend for now writing your own clock that conforms to C++11 official clock interface and wait for implementations to catch up. It will be much better approach than using OS specific API directly in your code. For Windows you can do it like that:</p> <pre><code>// Self-made Windows QueryPerformanceCounter based C++11 API compatible clock struct qpc_clock { typedef std::chrono::nanoseconds duration; // nanoseconds resolution typedef duration::rep rep; typedef duration::period period; typedef std::chrono::time_point&lt;qpc_clock, duration&gt; time_point; static bool is_steady; // = true static time_point now() { if(!is_inited) { init(); is_inited = true; } LARGE_INTEGER counter; QueryPerformanceCounter(&amp;counter); return time_point(duration(static_cast&lt;rep&gt;((double)counter.QuadPart / frequency.QuadPart * period::den / period::num))); } private: static bool is_inited; // = false static LARGE_INTEGER frequency; static void init() { if(QueryPerformanceFrequency(&amp;frequency) == 0) throw std::logic_error("QueryPerformanceCounter not supported: " + std::to_string(GetLastError())); } }; </code></pre> <p>For Linux it is even easier. Just read the man page of <code>clock_gettime</code> and modify the code above.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload