QueryPerformanceFrequency的单位

时间:2013-06-12 16:10:38

标签: windows api kernel

一个简单的问题:

QueryPerformanceFrequency单元是哪个? Hz(每秒滴答数)?

非常感谢, 布鲁诺

1 个答案:

答案 0 :(得分:3)

问:QueryPerformanceFrequency的单位?

答:KILO-HERTZ(不是Hz)

===========详情=================================== ===========

我的研究表明,计数器和频率都在KILO,KILO-clock-ticks和KILO-HERTZ!

计数器注册KILO-Clicks(KLICKS)并且freq是以kHz为单位或者我是UnderClocked。当你将Clock_Ticks除以Clock_Frequency,k 点击/(k 点击* sec ^ -1)时,除了几秒钟之外,所有内容都会消失。

以下是一个示例C程序,仅删除了基本要素:

#include "stdio.h"
#include <windows.h>   // Needed for LARGE_INTEGER

// gcc cpu.freq.test.c -o cft.exe
// cft.exe -> Sleep d_KLICKS=3417790, d_time=0.999182880 sec, CPU_Freq=3420585 KILO-Hz

void main(int argc, char *argv[])  {
    // Clock KILO-ticks start, end, CPU_Freq in kHz. KILOs cancel
    LARGE_INTEGER sklick, eklick, cpu_khz;  
    double delta_time;  // Expected time in SECONDS. All units above are k.

    QueryPerformanceFrequency(&cpu_khz);  // Gets clock KILO-tics, Klicks/sec
    QueryPerformanceCounter(&sklick);     // Capture cpu Start Klicks
    Sleep(1000);                          // Sleep 1000 MILLI-seconds
    QueryPerformanceCounter(&eklick);     // Capture cpu End   Klicks
    delta_time = (eklick.QuadPart-sklick.QuadPart) / (double)cpu_khz.QuadPart;
    printf("Sleep d_KLICKS=%lld, d_time=%4.9lf sec, CPU_Freq=%lld KILO-Hz\n", 
        eklick.QuadPart-sklick.QuadPart, delta_time, cpu_khz.QuadPart);  
}

它实际上编译了!运行...

Sleep d_KLICKS=3418803, d_time=0.999479036 sec, CPU_Freq=3420585 KILO-Hz

CPU freq读取3420585或3.420585E6或3.4 M-Hertz? &lt; - MEGA-HURTS!OUCH!

实际CPU频率为3.4 Mega-Kilo-Hz或3.4 GHz

microsoft似乎很困惑(有些事情永远不会改变): https://msdn.microsoft.com/en-us/library/windows/desktop/dn553408%28v=vs.85%29.aspx

QueryPerformanceFrequency(&Frequency); 
QueryPerformanceCounter(&StartingTime);
    // Activity to be timed
QueryPerformanceCounter(&EndingTime);
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
// We now have the elapsed number of ticks, along with the
// number of ticks-per-second.

&#34;经过的刻度数&#34;在1秒内是百万,而不是数十亿,所以他们不是单位CPU时钟 - 但是KILO-CPU-CLOCK-TICKS

FREQ相同的3个数量级错误:3.4百万不是&#34;每秒滴答&#34;但是每秒几万次。

只要你将另一个分开,点击就会以秒为单位取消。如果一个人如此愚蠢,以至于在他们的文件中采用ms并尝试使用他们的&#34;每秒嘀嗒声&#34;在其他一些计算中,你会结束1000倍或~1 standard_ms_error!

或许我们应该打电话给Heinrich来检查HIS单位?哎呀! 153年来不及。 :(