Archived

This topic is now archived and is closed to further replies.

Keermalec

QueryPerformanceCounter anyone?

Recommended Posts

Keermalec    122
QueryPerformanceCounter() is supposed to be much more precise than GetTickCount() on pc systems so I am tring to implement a timer using this function. Unfortunately, it does seem quite complex and I have not found any simple implementation on the net. Here is what I am trying to do:
struct Mtimer
{
	__int64		start;		// Start time, in counts

	__int64		end;		// End time, in counts

	__int64		freq;		// Count frequency (per second)

	__int64		elapsed;	// Time elapsed, in counts

};

Mtimer *mTimer;

void mInitTimer()
{
	QueryPerformanceFrequency(mTimer->freq);
	QueryPerformanceCounter(mTimer->start);
}

void mUpdateTimer()
{
	QueryPerformanceCounter(mTimer->end);
	mTimer->elapsed = mTimer->end - mTimer->start;
	mTimer->start = mTimer->end;
}
Of course, this does not work. It has to do with the QueryPerf.. functions needing a LARGE_INTEGER argument, but how does one convert from LARGE_INTEGER to __int64? [edited by - Keermalec on October 10, 2003 4:20:03 PM]

Share this post


Link to post
Share on other sites
Just create a LARGE_INTEGER and use the QuadPart component of its union (which is of type __int64) whenever you do calculations. I don''t recommend just type-casting an __int64 to a LARGE_INTEGER when passing it even though you should get the same results. Try not to use type-casting when it is not necessary.

Share this post


Link to post
Share on other sites
Keermalec    122
Thanks Polymorphic, I did as you suggested, but why do I get this?? (end time is always 7 and frequancy varies with start time, when it should be constant...)

Logfile created on Sat Oct 11 00:26:12 2003
-------------------------------------------
Timer start: -1638143324 end: 7 freq: 0 elapsed: 0
Timer start: -1638141830 end: 7 freq: -1638141830 elapsed: 7
Timer start: -1638068676 end: 7 freq: -1638068676 elapsed: 7
Timer start: -1638008203 end: 7 freq: -1638008203 elapsed: 7
Timer start: -1637950592 end: 7 freq: -1637950592 elapsed: 7


struct Mtimer
{
LARGE_INTEGER start; // Start variable, in counts

LARGE_INTEGER end; // End variable, in counts

LARGE_INTEGER freq; // Count frequency (per sec)

__int64 elapsed; // Time elapsed, in counts

};

Mtimer *mTimer;

void mInitTimer()
{
mTimer = new Mtimer;
QueryPerformanceFrequency( &mTimer->freq );
QueryPerformanceCounter( &mTimer->start );
mPrintLog();
}

void mUpdateTimer()
{
QueryPerformanceCounter( &mTimer->end );
mTimer->elapsed = mTimer->end.QuadPart - mTimer->start.QuadPart;
mTimer->start.QuadPart = mTimer->end.QuadPart;
mPrintLog();
}

void mPrintLog()
{
fprintf(mFile, "\nTimer start: %i end: %i freq: %i elapsed: %i",
mTimer->start.QuadPart,
mTimer->end.QuadPart,
mTimer->freq.QuadPart,
mTimer->elapsed );
}



[edited by - Keermalec on October 10, 2003 6:49:11 PM]

Share this post


Link to post
Share on other sites
CrazyMike    211
quote:
Original post by Keermalec
Thanks Polymorphic, I did as you suggested, but why do I get this?? (end time is always 7 and frequancy varies with start time, when it should be constant...)

Logfile created on Sat Oct 11 00:26:12 2003
-------------------------------------------
Timer start: -1638143324 end: 7 freq: 0 elapsed: 0
Timer start: -1638141830 end: 7 freq: -1638141830 elapsed: 7
Timer start: -1638068676 end: 7 freq: -1638068676 elapsed: 7
Timer start: -1638008203 end: 7 freq: -1638008203 elapsed: 7
Timer start: -1637950592 end: 7 freq: -1637950592 elapsed: 7


struct Mtimer
{
LARGE_INTEGER start; // Start variable, in counts

LARGE_INTEGER end; // End variable, in counts

LARGE_INTEGER freq; // Count frequency (per sec)

__int64 elapsed; // Time elapsed, in counts

};

Mtimer *mTimer;

void mInitTimer()
{
mTimer = new Mtimer;
QueryPerformanceFrequency( &mTimer->freq );
QueryPerformanceCounter( &mTimer->start );
mPrintLog();
}

void mUpdateTimer()
{
QueryPerformanceCounter( &mTimer->end );
mTimer->elapsed = mTimer->end.QuadPart - mTimer->start.QuadPart;
mTimer->start.QuadPart = mTimer->end.QuadPart;
mPrintLog();
}

void mPrintLog()
{
fprintf(mFile, "\nTimer start: %i end: %i freq: %i elapsed: %i",
mTimer->start.QuadPart,
mTimer->end.QuadPart,
mTimer->freq.QuadPart,
mTimer->elapsed );
}



[edited by - Keermalec on October 10, 2003 6:49:11 PM]


If I''m reading MSDN correctly,
the numbers may be printing incorrectly because the QuadParts are 64 bits and %i is for 32 bit numbers. So according to MSDN, try %I64i instead.

Share this post


Link to post
Share on other sites
Keermalec    122
OMG, CrazyMike, you are so right! %i is limited to 32-bit numbers and %I64i should be used instead. My timer now works beautifully fine, thanks a lot. Now why can I not find any reference to %I64i in my MSVC6 doc?...


MVE logfile created on Mon Oct 13 08:08:27 2003
-----------------------------------------------
Timer start: 8153306016 end: 0 freq: 3579545 elapsed: 0
Timer start: 8153307509 end: 8153307509 freq: 3579545 elapsed: 1493
Timer start: 8153412759 end: 8153412759 freq: 3579545 elapsed: 105250
Timer start: 8153475285 end: 8153475285 freq: 3579545 elapsed: 62526
Timer start: 8153529982 end: 8153529982 freq: 3579545 elapsed: 54697

Share this post


Link to post
Share on other sites