Computing cpu's clock in cycles per second

Wade Hampton wadehamptoniv at gmail.com
Mon Sep 27 14:34:49 UTC 2010


Did you look at using the RDTSC instruction to read the
cycle counter?

http://en.wikipedia.org/wiki/Time_Stamp_Counter

Sample this over an interval to get an estimate
of the clock frequency based on this counter.

__inline__ unsigned long long int rdtsc()
{
  unsigned long long int x;
  __asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
  return(x);
}

I use gettimeofday() calls to check the wall-clock time,
usleep(n) to sleep for a long time (second or more),
and rdtsc to compute the cycles....  Seems to work
well.

Cheers,
--
Wade Hampton


More information about the users mailing list