Sign in to follow this  
jimbogd

casting from LARGE_INTEGER?

Recommended Posts

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

Share this post


Link to post
Share on other sites
Cast from the parts of the union.


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.






Share this post


Link to post
Share on other sites
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:

// initialize the counter

double frequency; // global variable
LARGE_INTEGER iFrequency;
QueryPerformanceFrequency(&iFrequency);
// note that QueryPerformanceFrequency can fail if no high-res counter is available
// and returns 0
frequency = 1000.0 / iFrequency.QuadPart;
// frequency is now conversion between QueryXXX and our units (micro sec, milisec, seconds, up to you)


// to get the current time
LARGE_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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this