Re: [GD-Windows] More timer fun! (sorry for revisit!)
Brought to you by:
vexxed72
From: Josiah M. <jm...@ma...> - 2002-12-24 03:27:26
|
I don't know if you are going to think this is a good idea or not, but if you want a really high accuracy timer, it may be possible to get the number of actual CPU clock cycles elapsed and use that as your measurement. Of course this means that you have to use 64 bit integers, pay attention to the clock wrapping over, and limiting yourself to pentiums and higher, but that is a small price to pay. I think that it also won't work with laptops. The idea is pretty simple though. First get a reading of clock cycles, use the good old C time() function in a buisy loop for a second, then get another reading of clock cycles at your programs startup. The difference in clock cycles is the frequency of the cpu. Now, if you find any number of elapsed cycles, and divide by the frequency, you know how much time has passed to a high degree of accuracy. Lets see if I can dig out some code here. The following 2 code snippets should work, but the first is probably better. I got it out of a simple profiler I made recently. // 64 bit... yum _declspec(naked) __int64 _fastcall _profileTick() { _asm rdtsc _asm ret } // clock is 32 bit integer // this is a pentium specific macro that will count cpu cycles #define clocktick(clock) _asm push eax\ _asm push edx\ _asm rdtsc\ _asm mov clock, eax\ _asm pop edx\ _asm pop eax |