casting from LARGE_INTEGER?
I'm using C++ on Windows and I'm trying to write a class that picks the best "tick count" functionality available and returns the current tick.
I'm first checking for QueryPerformanceCounter(), then timeGetTime(), and finally GetTickCount(). Basically I want to use the best resolution timer available, but I'm not sure how to create a consistent class interface (or if its possible).
For example:
QueryPerformanceCounter() returns the tick in a LARGE_INTEGER.
timeGetTime() returns a DWORD.
GetTickCount() also returns a DWORD.
My class will have a function "get_tick()", which uses the version it decided was the best in the class constructor. I'd like the get_tick() function to return a value of the same time no matter what tick function was called. Now I'm pretty sure this can be done by casting, but I'm having trouble figuring out how to cast from the LARGE_INTEGER type.
Any advice would be appreciated. Hopefully I'm on the right lines...
thanks
jimbogd
Cast from the parts of the union.
LARGE_INTEGER li;
DWORD tick;
tick = li.LowPart;
Actually, you don't have to cast.
typedef union _LARGE_INTEGER { struct { DWORD LowPart; LONG HighPart;}; struct { DWORD LowPart; LONG HighPart; } u; __int64 QuadPart;} LARGE_INTEGER,*PLARGE_INTEGER;
LARGE_INTEGER li;
DWORD tick;
tick = li.LowPart;
Actually, you don't have to cast.
You can't cast as such, or better yet, LARGE_INTEGER is a 64-bit value, while the others are 32-bit.
Given LARGE_INTEGER x, calling x.QuadPart will give you a LONGLONG (64-bit int). You cannot directly cast this one since it contains time in some internal units.
So the proper way would be:
But whatever value you receive from QueryPerformanceCounter is not time until you convert it into your units.
Given LARGE_INTEGER x, calling x.QuadPart will give you a LONGLONG (64-bit int). You cannot directly cast this one since it contains time in some internal units.
So the proper way would be:
// initialize the counterdouble frequency; // global variableLARGE_INTEGER iFrequency;QueryPerformanceFrequency(&iFrequency);// note that QueryPerformanceFrequency can fail if no high-res counter is available// and returns 0frequency = 1000.0 / iFrequency.QuadPart;// frequency is now conversion between QueryXXX and our units (micro sec, milisec, seconds, up to you)// to get the current timeLARGE_INTEGER counter;QueryPerformanceCounter(&counter);return counter.QuadPart * frequency;// you can of course round the result into an int, rather than use the double returned
But whatever value you receive from QueryPerformanceCounter is not time until you convert it into your units.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement