title: How to benchmark in Qemu i386 system using rdtsc
body: <p>Currently I am trying to measure number of clock cycles taken to complete an operation by two different programming languages on same environment. (without using an OS)</p>
  
  <p>Currently I am using Qemu-i386 emulator and using rdtsc to measure the clock cycles.</p>
  
  <pre><code>/* Return the number of CPU ticks since boot. */
  static inline u64 rdtsc(void)
  {
      u32 hi, lo;
      // asm("cpuid");
      asm("rdtsc" : "=a" (lo), "=d" (hi));
      return ((u64) lo) | (((u64) hi) &lt;&lt; 32);
  }
  </code></pre>
  
  <p>Taking the difference between rdtsc before and after operation should provide the number of clock cycles.</p>
  
  <pre><code>    start_time = rdtsc();
      operation();
      stop_time = rdtsc();
      num_cycles = stop_time-start_time;
  </code></pre>
  
  <p>But the difference is not constant even when I take over 100s of iterations and varies by few thousands of cycles. </p>
  
  <ul>
  <li><p>Is there any better way of measuring clock cycles?</p></li>
  <li><p>Also is there any way of providing frequency as an input parameter in Qemu?
  Currently I am using</p></li>
  </ul>
  
  <p>qemu-system-i386  -kernel out.elf</p>