Search

Using "rdtsc" with gcc

Using the "rdtsc"

rdtsc is a time stamp counter that returns  the number of clock ticks from the time the system was last reset.
The rdtsc instruction returns the time stamp counter in EDX:EAX.

This instruction is useful when you want to measure the performance of a certain code or application in clock ticks, or compare the performance of two programs, which are too small to be counted in seconds.

Here is a sample C code that shows how you can make use of this instruction.



#include
#include
#include
#include
#include



void example() {

uint64_t tick1, tick2; //unsigned 64 bit quantity

unsigned c,d;

asm volatile("rdtsc" : "=a" (c), "=d" (d));  //assembly code running the instruction                        
                                                                  //rdtsc

tick1 = (((uint64_t)c) | (((uint64_t)d) << 32)); // calculating the tick value.

printf("time %llu",tick1);


}

Follow by Email