Use RDTSC instruction to measure time on x86 architecture

In x86 architecture, RDTSC instruction reads current time stamp counter from the hardware. This is a better cost measurement for a program than using GNU time. It’s overhead is low and accuracy is high. You can simply add the following function to get the current counter.

typedef unsigned long long cycles_t;
inline cycles_t currentcycles() {
    cycles_t result;
    __asm__ __volatile__ ("rdtsc" : "=A" (result));
    return result;
}

2 Responses to “Use RDTSC instruction to measure time on x86 architecture”

  1. John Smith Says:

    Sometimes exact UTC time is required. Ntp server steps in with powerful linux tools (like GPS NTP on linux)

  2. Jungwoo Ha Says:

    RDTSC is using the hardware clock. It indicates # of cycles, and not the time. Cycles are better indication of the time spent in an application, because of the granularity it can measure.

Leave a Reply