Calculating CPU frequency in C with RDTSC always returns 0

前端 未结 5 1113
情深已故
情深已故 2021-01-13 13:01

The following piece of code was given to us from our instructor so we could measure some algorithms performance:

#include 
#include 

        
5条回答
  •  我在风中等你
    2021-01-13 13:37

    You forgot to use volatile in your asm statement, so you're telling the compiler that the asm statement produces the same output every time, like a pure function. (volatile is only implicit for asm statements with no outputs.)

    This explains why you're getting exactly zero: the compiler optimized end-start to 0 at compile time, through CSE (common-subexpression elimination).

    See my answer on Get CPU cycle count? for the __rdtsc() intrinsic, and @Mysticial's answer there has working GNU C inline asm, which I'll quote here:

    // prefer using the __rdtsc() intrinsic instead of inline asm at all.
    uint64_t rdtsc(){
        unsigned int lo,hi;
        __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
        return ((uint64_t)hi << 32) | lo;
    }
    

    This works correctly and efficiently for 32 and 64-bit code.

提交回复
热议问题