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)
Currently I am using Qemu-i386 emulator and using rdtsc to measure the clock cycles.
/* 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) << 32);
}
Taking the difference between rdtsc before and after operation should provide the number of clock cycles.
start_time = rdtsc();
operation();
stop_time = rdtsc();
num_cycles = stop_time-start_time;
But the difference is not constant even when I take over 100s of iterations and varies by few thousands of cycles.
Is there any better way of measuring clock cycles?
Also is there any way of providing frequency as an input parameter in Qemu? Currently I am using
qemu-system-i386 -kernel out.elf