Measuring time with QPC

The free CAN Software API (Application Programming Interface) for Windows®
Locked
ValeVG
Posts: 4
Joined: Wed 10. Jun 2020, 15:52

Measuring time with QPC

Post by ValeVG » Wed 10. Jun 2020, 16:01

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

User avatar
M.Gerber
Design & Documentation
Design & Documentation
Posts: 65
Joined: Mon 13. Sep 2010, 16:34

Re: Measuring time with QPC

Post by M.Gerber » 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

ValeVG
Posts: 4
Joined: Wed 10. Jun 2020, 15:52

Re: Measuring time with QPC

Post by ValeVG » Mon 15. Jun 2020, 07:39

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

K.Wagner
Software Development
Software Development
Posts: 767
Joined: Wed 22. Sep 2010, 13:36

Re: Measuring time with QPC

Post by K.Wagner » Mon 15. Jun 2020, 08:56

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.
Best regards,
Keneth

ValeVG
Posts: 4
Joined: Wed 10. Jun 2020, 15:52

Re: Measuring time with QPC

Post by ValeVG » Mon 15. Jun 2020, 10:10

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

K.Wagner
Software Development
Software Development
Posts: 767
Joined: Wed 22. Sep 2010, 13:36

Re: Measuring time with QPC

Post by K.Wagner » Mon 15. Jun 2020, 10:20

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;
Best regards,
Keneth

ValeVG
Posts: 4
Joined: Wed 10. Jun 2020, 15:52

Re: Measuring time with QPC

Post by ValeVG » Mon 15. Jun 2020, 10:42

Yes, that was it. I get similar timings now. Thank you for your help!

K.Wagner
Software Development
Software Development
Posts: 767
Joined: Wed 22. Sep 2010, 13:36

Re: Measuring time with QPC

Post by K.Wagner » Mon 15. Jun 2020, 10:45

You're welcome. Closed
Best regards,
Keneth

Locked