Kamil
Kamil

Reputation: 13931

QueryPerformanceCounter and weird results

I want to use PerformanceCounter to measure how much time I need for some operation.

I don't know much about PerformanceCounter and C++ in general. I found some code here:

How to use QueryPerformanceCounter?

I'm getting weird results with this. Here is my try:

#include <Windows.h>

// ...

double PCFreq = 0.0;
__int64 CounterStart = 0;

void StartCounter()
{
    LARGE_INTEGER li;
    if (!QueryPerformanceFrequency(&li))
        printf("QueryPerformanceFrequency failed!\n");

    PCFreq = double(li.QuadPart) / 1000.0;

    //printf("Performance counter resolution: %f", PCFreq);

    QueryPerformanceCounter(&li);
    CounterStart = li.QuadPart;
}

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

int main(int argc, const char** argv) {
    while (true) {
        StartCounter();
        Sleep(1000); // just a test
        printf("Query frame: %d\n", GetCounter());

        // ...
    }
}

And here is my weird result with negative numbers:

enter image description here

What is wrong with my code?

Upvotes: 0

Views: 932

Answers (1)

Daniel
Daniel

Reputation: 31579

You print a double as a float, use %f:

printf("Query frame: %f\n", GetCounter());

Upvotes: 1

Related Questions