title: solution to rdtsc out of order execution?
body: <p>I am trying to replace clock_gettime(CLOCK_REALTIME, &amp;ts) with rdtsc to benchmark code execution time in terms of cpu cycles rather than server time. The execution time of the bench-marking code is critical for the software. I have tried running code on x86_64 3.20GHz ubuntu machine on an isolated core and got following numbers :</p>
  
  <p><strong><em>case 1 : clock get time :</em></strong> 24 nano seconds</p>
  
  <pre><code>void gettime(Timespec &amp;ts) {
          clock_gettime(CLOCK_REALTIME, &amp;ts);
  }
  </code></pre>
  
  <p><strong><em>case 2 : rdtsc (without mfence and compiler barrier) :</em></strong> 10 ns</p>
  
  <pre><code>void rdtsc(uint64_t&amp; tsc) {
          unsigned int lo,hi;
          __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
          tsc = ((uint64_t)hi &lt;&lt; 32) | lo;
  }
  </code></pre>
  
  <p><strong><em>case 3 : rdtsc (with mfence and compiler barrier) :</em></strong> 30 ns</p>
  
  <pre><code>void rdtsc(uint64_t&amp; tsc) {
          unsigned int lo,hi;
          __asm__ __volatile__ ("mfence;rdtsc" : "=a" (lo), "=d" (hi) :: "memory");
          tsc = ((uint64_t)hi &lt;&lt; 32) | lo;
  }
  </code></pre>
  
  <p>Issue here is I am aware of rdtsc being a non-serializing call and can be reordered by the CPU, an alternative is rdtscp which is a serializing call but instructions after rdtscp call can be reordered before rdtscp call. Using memory barrier is increasing the execution time.</p>
  
  <ul>
  <li>What is the most optimised and best way to benchmark a latency sensitive code ? </li>
  <li>Is there anyway to optimise the cases I mentioned ?</li>
  </ul>