赞 | 27 |
VIP | 0 |
好人卡 | 0 |
积分 | 23 |
经验 | 1750 |
最后登录 | 2023-5-4 |
在线时间 | 123 小时 |
Lv3.寻梦者
- 梦石
- 0
- 星屑
- 2309
- 在线时间
- 123 小时
- 注册时间
- 2017-2-26
- 帖子
- 36
|
控帧改为等待信号实现
写了个demo,验证windows下的WaitableTimer和STL sleep_for,结果令人很意外很失望。
源码:
- #include <Windows.h>
- #include <stdio.h>
- #include <thread>
- #include "SDL.h"
- int main(int argc, char* argv[])
- {
- uint64_t frequency = SDL_GetPerformanceFrequency();
- #if 1
- HANDLE waitable_timer = CreateWaitableTimerW(
- nullptr,
- TRUE,
- nullptr);
- while (waitable_timer)
- {
- uint64_t before = SDL_GetPerformanceCounter();
- LARGE_INTEGER delay;
- delay.QuadPart = -1E7 / 60;
- SetWaitableTimer(
- waitable_timer,
- &delay,
- 0, nullptr, nullptr, FALSE
- );
- WaitForSingleObject(waitable_timer, INFINITE);
- uint64_t after = SDL_GetPerformanceCounter();
- printf("call: %lld, real: %llu, error: %lld\n", -delay.QuadPart, after - before, llround(0.L + after - before + delay.QuadPart));
- }
- #else
- while (true)
- {
- uint64_t before = SDL_GetPerformanceCounter();
- long long delay = 1E7 / 60;
- std::this_thread::sleep_for(std::chrono::nanoseconds(delay * 100));
- uint64_t after = SDL_GetPerformanceCounter();
- printf("call: %lld, real: %llu, error: %lld\n", delay, after - before, llround(0.L + after - before - delay));
- }
- #endif
- return 0;
- }
复制代码
运行结果:
WaitableTimer
STL sleep_for
结论:
WaitableTimer的精度实际上和STL sleep_for一样,都是15ms左右(具体数字取决于操作系统的调度时间片)的粒度。
P.S.:github不好发图,就借楼发了 |
评分
-
查看全部评分
|