title: Is rdtsc timer inaccurate in linux?
body: <pre><code> __inline__ uint64_t rdtsc() {
      uint32_t low, high;
      __asm__ __volatile__ (
          "xorl %%eax,%%eax \n    cpuid"
          ::: "%rax", "%rbx", "%rcx", "%rdx" );
      __asm__ __volatile__ (
                            "rdtsc" : "=a" (low), "=d" (high));
      return (uint64_t)high &lt;&lt; 32 | low;
  }
  </code></pre>
  
  <p>I have used the above rdtsc function as a timer in my program:
  The following code results in 312-344 clock cycles:</p>
  
  <pre><code> start = rdtsc();
   stop = rdtsc();
  
   elapsed_ticks = (unsigned)((stop-start));
   printf("\n%u ticks\n",elapsed_ticks);
  </code></pre>
  
  <p>every time I run the above code I get different values. Why is that? </p>
  
  <p>I ran the same code in Visual C++ which uses an rdtsc function in "intrin.h". I was getting a constant value of 18 clocks.Yes, it was constant on every run! Can someone please explain? Thanks!</p>