Simple clock counter attempt:

Started by
10 comments, last by gimp 23 years, 9 months ago
Hrmmm... thats interesting... I might look at that too...

In the meantime I''ve expanded the app to a little class:
    class CClock{public:	CClock();	~CClock();	//Start the timer	inline void Reset(void);	//Obtain the timer time since last reset.	inline unsigned long ElapsedMilliseconds(void);	inline unsigned long ElapsedClockCycles(void);private:	__int64 Freq; //Clock cycles per millisecond.	__int64 Start;	__int64 End;	__int64 Delta;	__int64 Overhead;}; CClock::CClock(){	__int64 Freq	=0; //Clock cycles per millisecond.	__int64 Start	=0;	__int64 End		=0;	__int64 Delta	=0;	__int64 Overhead=0;	//If this fails the frequency (freq) will remain 0.	QueryPerformanceFrequency((LARGE_INTEGER*)&Freq);		//Calculate Overhead	Reset();	ElapsedClockCycles();	Overhead = Delta;}inline void CClock::Reset(void){	Sleep(0);	//A thread can relinquish the remainder of its time slice by calling 				//this function with a sleep time of zero milliseconds. We do this to 				//ensure short timings arn''t interupted by the scheduler. (Average				//desktop schedule time is 40ms)	if (Freq)	{		__int64 tempStart=0;		__asm			{			__emit 0x0f	// RDTSC - read time-stamp counter					__emit 0x31					mov dword ptr [tempStart],eax					mov dword ptr [tempStart+4],edx			}			Start=tempStart;	}	else		Start = (__int64)(GetTickCount()*1000);}inline unsigned long CClock::ElapsedMilliseconds(void){	if (Freq)	{		__int64 tempEnd=0;		__asm			{			__emit 0x0f	// RDTSC - read time-stamp counter					__emit 0x31					mov dword ptr [tempEnd],eax					mov dword ptr [tempEnd+4],edx			}			End=tempEnd;	}	else 		End = (__int64)(GetTickCount()*1000);	return (unsigned long)(1000*(End-Start));}inline unsigned long CClock::ElapsedClockCycles(void){	__int64 tempEnd=0;	__asm		{		__emit 0x0f	// RDTSC - read time-stamp counter				__emit 0x31				mov dword ptr [tempEnd],eax				mov dword ptr [tempEnd+4],edx		}		End=tempEnd;	Delta = End - Start - Overhead;	return (unsigned long)(Delta);}void main (void){	CClock *Clock = new CClock;	Clock->Reset();	cout << Clock->ElapsedClockCycles();}    


However this returns grossly incorrect results in release mode. Now since I added a little more code now it''s grossly wrong in debug as well. I *think* that when you get problems between release and debug it means uninitialised variables.

I performed a test. The Freq variable in release mode doesn''t seem to retain it''s data once I leave the constructor... Whats up with that? Its a class scope variable???

The other problem I seem to have here is that My class private data is not available from within the assembler. Should I be able to do that?

gimp
Advertisement
Hey win32mfc guy - is there anyway I can download the contents of your head for reading later...?

This topic is closed to new replies.

Advertisement