What OS/compiler are you using? Here are a couple of examples that I grabbed from google:
unsigned long long read_timer() { _asm { rdtsc } }
inline __int64 __fastcall RDTSC( )
{
_asm{
rdtsc
}
}
void
GetTimeNT( u64* t )
{
u32 lo, hi;
// Read the Time-Stamp Counter register into 'lo' and 'hi.
__asm
{
// If this is the Metrowerks compiler.
#ifdef __MWERKS__
rdtsc
#else // Visual C++ Compiler doesn't recognize the 'rdtsc'
// instruction so the opcodes are used instead.
__emit 0fh
__emit 031h
#endif
mov lo, eax
mov hi, edx
}
// Combine the low and high 32-bit parts into a single
// 64-bit number and return the result.
*t = ( (u64) hi << 32 ) | (u64) lo;
}
The value returned is a 64-bit counter that is incremented by the system clock. So on a 500 MHz Pentium II, each tick is 2 nS. To use, do something like:
int
main(int argc, char **argv)
{
__int64 t1, t2;
long elapsed;
/* start measurement */
t1 = RDTSC();
/* call code under test */
foo();
/* stop measurement */
t2 = RDTSC();
/* compute elapsed time in microseconds, 500 MHz CPU */
elapsed = (long) ((t2 - t1)/500);
return 0;
};
54º40' or Fight!
[ Parent ]