有的时候需要精确测量,从阅读别人的代码中学习到一些。可以在内核态调用,精确计算起止时间。
#ifndef _HRTIME_H_121AJ1
#define _HRTIME_H_121AJ1
#ifndef _HRTIME_CPUFREQ
# define _HRTIME_CPUFREQ 2500 /* GHz */
#endif
#define HRTIME_NS2CYCLE(__ns) ((__ns) * _HRTIME_CPUFREQ / 1000)
#define HRTIME_CYCLE2NS(__cycles) ((__cycles) * 1000 / _HRTIME_CPUFREQ)
typedef unsigned long long hrtime_t;
static inline void hrtime_barrier() {
asm volatile( "cpuid" :::"rax", "rbx", "rcx", "rdx");
}
#if defined(__i386__)
static inline unsigned long long hrtime_cycles(void)
{
unsigned long long int x;
__asm__ volatile (".byte 0x0f, 0x31" : "=A" (x));
return x;
}
#elif defined(__x86_64__)
static inline unsigned long long hrtime_cycles(void)
{
unsigned hi, lo;
__asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
}
#else
#error "What architecture is this???"
#endif