Timer function to provide time in nano seconds using C++

前端 未结 16 2044
野趣味
野趣味 2020-11-22 06:02

I wish to calculate the time it took for an API to return a value. The time taken for such an action is in the space of nano seconds. As the API is a C++ class/function, I a

16条回答
  •  误落风尘
    2020-11-22 06:28

    You can use the following function with gcc running under x86 processors:

    unsigned long long rdtsc()
    {
      #define rdtsc(low, high) \
             __asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high))
    
      unsigned int low, high;
      rdtsc(low, high);
      return ((ulonglong)high << 32) | low;
    }
    

    with Digital Mars C++:

    unsigned long long rdtsc()
    {
       _asm
       {
            rdtsc
       }
    }
    

    which reads the high performance timer on the chip. I use this when doing profiling.

提交回复
热议问题