久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使用 QueryPerformanceCounter?

How to use QueryPerformanceCounter?(如何使用 QueryPerformanceCounter?)
本文介紹了如何使用 QueryPerformanceCounter?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我最近決定我的 Timer 類需要從使用毫秒更改為微秒,經過一些研究后,我認為 QueryPerformanceCounter 可能是我最安全的選擇.(Boost::Posix 上的警告說它可能不適用于 Win32 API,這讓我有點失望).但是,我不確定如何實現它.

I recently decided that I needed to change from using milliseconds to microseconds for my Timer class, and after some research I've decided that QueryPerformanceCounter is probably my safest bet. (The warning on Boost::Posix that it may not works on Win32 API put me off a bit). However, I'm not really sure how to implement it.

我正在做的是調用我正在使用的任何 GetTicks() esque 函數并將其分配給 Timer 的 startingTicks 變量.然后為了找到經過的時間量,我只需從 startingTicks 中減去函數的返回值,當我重置計時器時,我只需再次調用該函數并將startingTicks 分配給它.不幸的是,從我看到的代碼來看,它并不像調用 QueryPerformanceCounter() 那樣簡單,而且我不確定我應該將什么作為參數傳遞.

What I'm doing is calling whatever GetTicks() esque function I'm using and assigning it to Timer's startingTicks variable. Then to find the amount of time passed I just subtract the function's return value from the startingTicks, and when I reset the timer I just call the function again and assign startingTicks to it. Unfortunately, from the code I've seen it isn't as simple as just calling QueryPerformanceCounter(), and I'm not sure what I'm supposed to pass as its argument.

推薦答案

#include <windows.h>

double PCFreq = 0.0;
__int64 CounterStart = 0;

void StartCounter()
{
    LARGE_INTEGER li;
    if(!QueryPerformanceFrequency(&li))
    cout << "QueryPerformanceFrequency failed!
";

    PCFreq = double(li.QuadPart)/1000.0;

    QueryPerformanceCounter(&li);
    CounterStart = li.QuadPart;
}
double GetCounter()
{
    LARGE_INTEGER li;
    QueryPerformanceCounter(&li);
    return double(li.QuadPart-CounterStart)/PCFreq;
}

int main()
{
    StartCounter();
    Sleep(1000);
    cout << GetCounter() <<"
";
    return 0;
}

這個程序應該輸出一個接近 1000 的數字(windows sleep 不是那么準確,但應該是 999).

This program should output a number close to 1000 (windows sleep isn't that accurate, but it should be like 999).

StartCounter() 函數記錄性能計數器在 CounterStart 變量中的滴答數.GetCounter() 函數返回自 StartCounter() 上次調用以來的毫秒數作為雙精度值,所以如果 GetCounter() 返回 0.001 那么自從 StartCounter() 被調用以來已經過去了大約 1 微秒.

The StartCounter() function records the number of ticks the performance counter has in the CounterStart variable. The GetCounter() function returns the number of milliseconds since StartCounter() was last called as a double, so if GetCounter() returns 0.001 then it has been about 1 microsecond since StartCounter() was called.

如果您想讓計時器使用秒,請更改

If you want to have the timer use seconds instead then change

PCFreq = double(li.QuadPart)/1000.0;

PCFreq = double(li.QuadPart);

或者如果你想要微秒那么使用

or if you want microseconds then use

PCFreq = double(li.QuadPart)/1000000.0;

但實際上是為了方便,因為它返回一個雙精度值.

But really it's about convenience since it returns a double.

這篇關于如何使用 QueryPerformanceCounter?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(將 RGB 轉換為 HSV 并將 HSV 轉換為 RGB 的算法,范圍為 0-255)
How to convert an enum type variable to a string?(如何將枚舉類型變量轉換為字符串?)
When to use inline function and when not to use it?(什么時候使用內聯函數,什么時候不使用?)
Examples of good gotos in C or C++(C 或 C++ 中好的 goto 示例)
Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);(ios_base::sync_with_stdio(false) 的意義;cin.tie(NULL);)
Is TCHAR still relevant?(TCHAR 仍然相關嗎?)
主站蜘蛛池模板: 国产精品伦一区二区三级视频 | 香蕉久久网| av一区二区在线观看 | 91精品国产色综合久久 | 午夜色婷婷 | 91xxx在线观看 | 欧美 日韩 在线播放 | 色综合av| 看av电影 | 亚洲精品1| 国产高清性xxxxxxxx | 天天拍天天插 | 在线观看国产视频 | 亚洲精品久久久一区二区三区 | 欧美国产日韩一区二区三区 | 成人h动漫亚洲一区二区 | www.黄色网| xx视频在线 | 亚洲区中文字幕 | 99re在线视频 | 91精品欧美久久久久久久 | 91视视频在线观看入口直接观看 | av在线二区 | 亚洲免费一区二区 | av国产在线观看 | 久久综合久色欧美综合狠狠 | 男人的天堂avav | 免费观看一级黄色录像 | 视频在线一区二区 | 亚洲区中文字幕 | 91手机精品视频 | 综合激情网 | 国产欧美一区二区三区在线看 | 精品国产乱码久久久久久1区2区 | 在线精品一区二区三区 | 在线观看视频亚洲 | 欧一区二区 | 国产在线一区二区三区 | 91久久久精品国产一区二区蜜臀 | 国产美女黄色 | 久久精品16 |