You can use RDTSC,
which
is the processor counter. It is updated every clock cycle. It is not a
serializing instruction, so if you want a time stamp after all the work
is done, also use a serializing instruction like CPUID.
For the case below you
do not need it, but you will if you are timing a piece of code.
Also, this will not
work well on laptops running on battery.
Here is some example code for precise counters. Just include this .h
file and use macros to start, stop and get time.
#define _USE_COUNTERS_
#ifndef _PERF_TIMERS_
#define _PERF_TIMERS_
#ifdef
_USE_COUNTERS_
#define INIT_COUNTER
unsigned __int64 start, end; \
void *pStart = &start; \
void *pEnd = &end; \
char pClockTickBuf[100];
#define START_COUNTER \
{_asm mov eax, 0 \
_asm cpuid \
_asm rdtsc \
_asm mov ebx, pStart \
_asm mov dword ptr[ebx], eax \
_asm mov dword ptr[ebx+4], edx \
_asm mov eax, 0 \
_asm cpuid}
#define STOP_COUNTER \
{_asm mov eax, 0 \
_asm cpuid \
_asm rdtsc \
_asm mov ebx, pEnd \
_asm mov dword ptr[ebx], eax \
_asm mov dword ptr[ebx+4], edx \
_asm mov eax, 0 \
_asm cpuid}
#define GET_COUNT(buf) \
{_ui64toa(end-start,buf,10);}
#define GET_COUNT_MS(buf) \
{_ui64toa((end-start)/3000000,buf,10);}
#else
#define INIT_COUNTER
#define START_COUNTER
#define STOP_COUNTER
#define GET_COUNT(buf)
#endif //
_USE_COUNTERS_
#endif //_PERF_TIMERS_