//代码来自kbengine
*nux:
inline uint64 timestamp()
{
#ifdef KBE_USE_RDTSC
return timestamp_rdtsc();
#else // KBE_USE_RDTSC
if (g_timingMethod == RDTSC_TIMING_METHOD)
return timestamp_rdtsc();
else if (g_timingMethod == GET_TIME_OF_DAY_TIMING_METHOD)
return timestamp_gettimeofday();
else //if (g_timingMethod == GET_TIME_TIMING_METHOD)
return timestamp_gettime();
#endif // KBE_USE_RDTSC
}
inline uint64 timestamp_rdtsc()
{
uint32 rethi, retlo;
__asm__ __volatile__ (
"rdtsc":
"=d" (rethi),
"=a" (retlo)
);
return uint64(rethi) << 32 | retlo;
}
// 使用 gettimeofday. 测试大概比RDTSC20倍-600倍。
// 此外,有一个问题
// 2.4内核下,连续两次调用gettimeofday的可能
// 返回一个结果是倒着走。
#include <sys/time.h>
inline uint64 timestamp_gettimeofday()
{
timeval tv;
gettimeofday( &tv, NULL );
return 1000000ULL * uint64( tv.tv_sec ) + uint64( tv.tv_usec );
}
#include <time.h>
#include <asm/unistd.h>
inline uint64 timestamp_gettime()
{
timespec tv;
KBE_VERIFY(syscall( __NR_clock_gettime, CLOCK_MONOTONIC, &tv ) == 0);
return 1000000000ULL * tv.tv_sec + tv.tv_nsec;
}
win:
#ifdef KBE_USE_RDTSC
#pragma warning (push)
#pragma warning (disable: 4035)
inline uint64 timestamp()
{
__asm rdtsc
}
#pragma warning (pop)
#else // KBE_USE_RDTSC
#include <windows.h>
inline uint64 timestamp()
{
LARGE_INTEGER counter;
QueryPerformanceCounter( &counter );
return counter.QuadPart;
}
#endif // KBE_USE_RDTSC
转载于:https://my.oschina.net/invictuslee/blog/336090