Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- #include <windows.h>
- __int64 getSystemTimeMicroseconds() {
- LARGE_INTEGER ticks;
- QueryPerformanceCounter(&ticks);
- return *(__int64*)&ticks;
- }
- int main() {
- HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL);
- const int numTrials = 100;
- __int64 microsecondsLateSum = 0;
- for (int i = 0; i < numTrials; i++) {
- __int64 timeBeforeWaitMicroseconds = getSystemTimeMicroseconds();
- __int64 desiredWaitMicroseconds = rand() * 1337 % 2600000 + 400000;
- __int64 desiredTimeAfterWaitMicroseconds = timeBeforeWaitMicroseconds + desiredWaitMicroseconds;
- WaitForMultipleObjects(1, &event, false, desiredWaitMicroseconds / 1000);
- __int64 timeAfterWaitMicroseconds = getSystemTimeMicroseconds();
- __int64 microsecondsLate = timeAfterWaitMicroseconds - desiredTimeAfterWaitMicroseconds;
- std::cout << "from " << timeBeforeWaitMicroseconds << " desired wait of " << desiredWaitMicroseconds << " to desired end time of " << desiredTimeAfterWaitMicroseconds << " actually ended " << timeAfterWaitMicroseconds << " late by " << microsecondsLate << " microseconds" << std::endl;
- microsecondsLateSum += microsecondsLate;
- }
- std::cout << "late by on average " << microsecondsLateSum / numTrials << " microseconds" << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement