0

A simple question:

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

Thank you very much, Bruno

bgarate
  • 116
  • 2
  • 9

1 Answers1

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. :(

BrianP007
  • 162
  • 12
  • 8
    this answer is misleading: the resolution of the counters is given by QueryPerformanceFrequency (as in 'number of ticks per second'), this is the actual answer here. There is no guarantee whatsoever that the (hardware, much less its software implementation by microsoft) timers resolution would directly relate to the CPU operating frequency. – Florian Castellane Sep 18 '18 at 07:33