Pentium Time Stamp Counter

On a Pentium, you are able to get the number of clock cycles since the CPU was powered up or reset. Determining the difference between two polls of the CPU for the number of clock cycles is a very effective way to time your code. The rdtsc instruction is used to move the 64-bit counter value into the registers EDX:EAX. Below is a piece of code that will return the 64-bit wide counter value. Note that this should work on both i386 and x86_64 architectures.

unsigned long long int rdtsc(void)
   unsigned long long int x;
   unsigned a, d;

   __asm__ volatile("rdtsc" : "=a" (a), "=d" (d));

   return ((unsigned long long)a) | (((unsigned long long)d) << 32);;

This function will return a single "unsigned long long int" value that is the 64-bit wide counter.