programming, .NET, linux and others

Read time stamp counter in C

by Gregor Uhlenheuer on May 31, 2010

While searching for a method to benchmark/profile some functions in C I stumbled upon the RDTSC 1. The TSC is a specific register available at least on x86 processors since the Pentium which counts the processor ticks since reset. The TSC is returned on the RDTSC call (opcode 0F 31) in EDX:EAX.

The following C function worked for me on x86_64 linux:

static inline unsigned long rdtsc()
    unsigned int lo, hi;
    __asm__ __volatile__("rdtsc" : "=a" (lo), "=d" (hi));
    return (unsigned long) hi << 32 | lo;

  1. Read Time Stamp Counter

This post is tagged with c, cpu, profiling and programming