Page 1 of 1

Measuring time with QPC

Posted: Wed 10. Jun 2020, 16:01
by ValeVG
Hi,
I read some forum posts where you specify that you use QueryPerformanceCounter and QueryPerformanceFrequency to measure timestamps. I tried to do the same, but I get different timestamps. Which part am I doing wrong?

My code is:
send CAN frame and save current time point with code:

Code: Select all

LARGE_INTEGER EndingTime, ElapsedMicroseconds;
        LARGE_INTEGER Frequency;
        QueryPerformanceFrequency(&Frequency);
        QueryPerformanceCounter(&EndingTime);
        ElapsedMicroseconds.QuadPart = (EndingTime.QuadPart * 1000000) / Frequency.QuadPart;
        cout << "time point: " << ElapsedMicroseconds.QuadPart << endl;
Then I get response message in couple milliseconds and I read its timestamp with code:

Code: Select all

                r = pcan->Read(pcan->channel, &receivedMsg, &timestamp);
                if (r == PCAN_ERROR_OK)
                    microsTimestamp = timestamp.micros + 1000 * timestamp.millis + 0x100000000 * 1000 * timestamp.millis_overflow;
                cout << microsTimeStamp << endl;
Output looks like this:
time point: 202831938021
968478406
Which is wrong, even from the number of digits. I hope you can help me. :)

Re: Measuring time with QPC

Posted: Thu 11. Jun 2020, 09:27
by M.Gerber
Hello ValeVG,

We have a holiday here in Germany, thus please be patient until the answer to your problem (anticipated tomorrow, Friday).

Thanks,
Mark

Re: Measuring time with QPC

Posted: Mon 15. Jun 2020, 07:39
by ValeVG
M.Gerber wrote:
Thu 11. Jun 2020, 09:27
Hello ValeVG,

We have a holiday here in Germany, thus please be patient until the answer to your problem (anticipated tomorrow, Friday).

Thanks,
Mark
Sure thing, enjoy your holidays. :)

Re: Measuring time with QPC

Posted: Mon 15. Jun 2020, 08:56
by K.Wagner
Hello,

As far as I can see, the value you are calculating is being truncated to a 32-bit value. Is microsTimestamp also a large integer? Please note that you have to do your operations using 64-bit variables.

Re: Measuring time with QPC

Posted: Mon 15. Jun 2020, 10:10
by ValeVG
Sorry for not including whole code in first comment. It's defined as

Code: Select all

long long int microsTimestamp;
.

If I do:

Code: Select all

microsTimestamp = timestamp.micros + 1000 * timestamp.millis + 0x100000000 * 1000 * timestamp.millis_overflow;
std::cout << "sizeof: "<< sizeof(microsTimestamp) << std::endl;
It prints
sizeof: 8

Re: Measuring time with QPC

Posted: Mon 15. Jun 2020, 10:20
by K.Wagner
Hello,

truncation can also occur, when in a calculation no type specification is done. Try the calculation like this:

Code: Select all


    UINT64 micros = 0L;

    micros = UINT64(ts.millis) * 1000 + ts.micros + 0x100000000 * 1000 * ts.millis_overflow;
	
    return micros;

Re: Measuring time with QPC

Posted: Mon 15. Jun 2020, 10:42
by ValeVG
Yes, that was it. I get similar timings now. Thank you for your help!

Re: Measuring time with QPC

Posted: Mon 15. Jun 2020, 10:45
by K.Wagner
You're welcome. Closed