久久久久久久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| 国产一区久久 | 一区二区在线观看av | 婷婷激情综合 | 成人精品系列 | 亚洲日本免费 | 午夜一区| 久久久久久久久综合 | 天天操精品视频 | 中文字幕欧美一区 | 日韩中文字幕视频在线观看 | av永久免费 | 在线国产一区二区三区 | 国产欧美精品一区二区三区 | 日本中文字幕一区 | 亚洲精品一区二区三区蜜桃久 | 亚洲精品乱码久久久久久9色 | 色综合久久久久 | 在线观看中文字幕av | 午夜免费小视频 | 国产伊人精品 | 一级特黄色毛片 | 国产成人久久久 | 久久精品国产一区二区三区 | 国产福利91精品一区二区三区 | 中文字幕在线剧情 | 久久久青草婷婷精品综合日韩 | 懂色av色香蕉一区二区蜜桃 | 久久久亚洲 | 激情综合五月天 | 综合色在线 | caoporn免费在线视频 | 亚洲免费久久久 | 国产精品欧美一区喷水 | 美国黄色一级片 | 欧美日韩综合 |