Reputation: 10584
Checkout the below code:
tick1 = GetTickCount();
Sleep (100);
tick2 = GetTickCount ();
The tick2-tick1 most of times comes less than 100. My expectation is it should always 100+. Is my expectation wrong?
Upvotes: -1
Views: 675
Reputation: 11311
Looks like yours (and mine) expectations ARE wrong.
I used better timing:
chrono::time_point<std::chrono::high_resolution_clock> tick1, tick2;
std::chrono::milliseconds timespan(100); // or whatever
for (int i = 0; i < 100; ++i)
{
tick1 = chrono::high_resolution_clock::now();
Sleep(100);
tick2 = chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed_seconds = tick2 - tick1;
cout << elapsed_seconds.count() << endl;
}
The majority of intervals are 0.099XXX with 2 or 3 (out of 100) 0.1000XXX
The std::this_thread::sleep_for(...) recommended in Precise thread sleep needed. Max 1ms error has similar results. But the error is less than 1ms. Do you need better precision?
Upvotes: 0