如何使用QueryPerformanceCounter?我最近決定,我的計時器類需要從使用毫秒改為微秒,經過一些研究后,我認為QueryPerformanceCounter可能是我最安全的賭注。(對.的警告Boost::Posix它可能不能在Win 32 API上工作,這讓我有點猶豫)。但是,我不太確定如何實現它。我所做的就是管它叫什么GetTicks()我正在使用的esque函數,并將其分配給Timer的startingTicks變量。然后,為了找出傳遞的時間,我只需將函數的返回值從startingTicks,當我重置計時器時,我只需再次調用函數并將startingTick分配給它。不幸的是,從我看到的代碼來看,它并不像調用QueryPerformanceCounter(),我不知道我應該通過什么作為它的論點。
3 回答

慕哥9229398
TA貢獻1877條經驗 獲得超6個贊
/** Use to init the clock */#define TIMER_INIT \ LARGE_INTEGER frequency; \ LARGE_INTEGER t1,t2; \ double elapsedTime; \ QueryPerformanceFrequency(&frequency);/** Use to start the performance timer */#define TIMER_START QueryPerformanceCounter(&t1);/** Use to stop the performance timer and output the result to the standard stream. Less verbose than \c TIMER_STOP_VERBOSE */#define TIMER_STOP \ QueryPerformanceCounter(&t2); \ elapsedTime=(float)(t2.QuadPart-t1.QuadPart)/frequency.QuadPart; \ std::wcout<<elapsedTime<<L" sec"<<endl;
TIMER_INIT{ TIMER_START Sleep(1000); TIMER_STOP}{ TIMER_START Sleep(1234); TIMER_STOP}
1.00003 sec 1.23407 sec
- 3 回答
- 0 關注
- 1174 瀏覽
添加回答
舉報
0/150
提交
取消