Calculating CPU frequency in C with RDTSC always returns 0

前端 未结 5 1083
情深已故
情深已故 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:33

    I can't say for certain what exactly is wrong with your code, but you're doing quite a bit of unnecessary work for such a simple instruction. I recommend you simplify your rdtsc code substantially. You don't need to do 64-bit math carries your self, and you don't need to store the result of that operation as a double. You don't need to use separate outputs in your inline asm, you can tell GCC to use eax and edx.

    Here is a greatly simplified version of this code:

    #include 
    
    uint64_t rdtsc() {
        uint64_t ret;
    
    # if __WORDSIZE == 64
        asm ("rdtsc; shl $32, %%rdx; or %%rdx, %%rax;"
            : "=A"(ret)
            : /* no input */
            : "%edx"
        );
    #else
        asm ("rdtsc" 
            : "=A"(ret)
        );
    #endif
        return ret;
    }
    

    Also you should consider printing out the values you're getting out of this so you can see if you're getting out 0s, or something else.

提交回复
热议问题