10 timer() : time_(timeGetTime()){}
\r
14 return static_cast<double>(timeGetTime() - time_)/1000.0;
\r
19 time_ = timeGetTime();
\r
26 class high_prec_timer
\r
31 QueryPerformanceFrequency(&freq_);
\r
32 QueryPerformanceCounter(&time_);
\r
35 // Author: Ryan M. Geiss
\r
36 // http://www.geisswerks.com/ryan/FAQS/timing.html
\r
37 void tick(double interval)
\r
40 QueryPerformanceCounter(&t);
\r
42 if (time_.QuadPart != 0)
\r
44 int ticks_to_wait = static_cast<int>(static_cast<double>(freq_.QuadPart) * interval);
\r
48 QueryPerformanceCounter(&t);
\r
50 int ticks_passed = static_cast<int>(static_cast<__int64>(t.QuadPart) - static_cast<__int64>(time_.QuadPart));
\r
51 int ticks_left = ticks_to_wait - ticks_passed;
\r
53 if (t.QuadPart < time_.QuadPart) // time wrap
\r
55 if (ticks_passed >= ticks_to_wait)
\r
60 // if > 0.002s left, do Sleep(1), which will actually sleep some
\r
61 // steady amount, probably 1-2 ms,
\r
62 // and do so in a nice way (cpu meter drops; laptop battery spared).
\r
63 // otherwise, do a few Sleep(0)'s, which just give up the timeslice,
\r
64 // but don't really save cpu or battery, but do pass a tiny
\r
66 if (ticks_left > static_cast<int>((freq_.QuadPart*2)/1000))
\r
69 for (int i = 0; i < 10; ++i)
\r
70 Sleep(0); // causes thread to give up its timeslice
\r
79 LARGE_INTEGER freq_;
\r
80 LARGE_INTEGER time_;
\r