Units of QueryPerformanceFrequency

半城伤御伤魂 提交于 2019-12-25 05:31:50

问题


A simple question:

Which is the QueryPerformanceFrequency unit? Hz (ticks per second)?

Thank you very much, Bruno


回答1:


Q: Units of QueryPerformanceFrequency?

A: KILO-HERTZ (NOT Hz)

=========== DETAILS ==============================================

My research indicates that both Counters and Freq are in KILOs, KILO-clock-ticks and KILO-HERTZ!

The counters register KILO-Clicks (KLICKS) and the freq is either in kHz or I am woefully UnderClocked. When you divide the Clock_Ticks by Clock_Frequency, kclicks/(kclicks*sec^-1), everything wipes out except for seconds.

Here is an example C program stripped to just the essentials:

#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);  
}

It actually compiles! Running...

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

The CPU freq reads 3420585 or 3.420585E6 or 3.4 M-Hertz? <- MEGA-HURTS !OUCH!

The actual CPU freq is 3.4 Mega-Kilo-Hz or 3.4 GHz

microsoft appears to be confused (some things Never Change): 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.

The number of "elapsed ticks" in 1 second is in the MILLIONS, NOT BILLIONS so they are NOT UNIT-CPU-CLOCK-TICKS but KILO-CPU-CLOCK-TICKS

Same off-by-3-orders-of-magnitude error for FREQ: 3.4 MILLION is not "ticks-per-second" but THOUSAND-ticks-per-second.

As long as you divide one by the other, the ?clicks cancel with a result in seconds. If one were so fatuous as to take ms at their document and try to use their "ticks-per-second" in some other calculation, you would wind up off by a factor of 1000 or ~1 standard_ms_error!

Perhaps we should call Heinrich in to check HIS units? Oops! 153 years too late. :(



来源:https://stackoverflow.com/questions/17070341/units-of-queryperformancefrequency

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!