Skip to main content
Email Password   helpLost your password?

Introduction

Everyone has used good old time(NULL) to get timings accurate to the second. Some of you may have used GetSystemTime() to get sub-second timings. The really clever have found QueryPerformanceFrequency and QueryPerformanceCounter, which give timings accurate to a millisecond or better (for the record, I'm not among the really clever - I found out about those two by reading the Python documentation... Thanks, Guido!).

But if you really want accurate timings, this code will give you timings accurate to the machine cycle, which on a 1 ghz machine is one nanosecond. On a 2 ghz machine, it's 1/2 nanosecond. Old 100 mhz machines will only get 10-nanosecond timings. You get the idea.

It requires a tiny bit of assembler, but it's worth it:

__int64 GetMachineCycleCount()
{      
   __int64 cycles;
   _asm rdtsc; // won't work on 486 or below - only pentium or above

   _asm lea ebx,cycles;
   _asm mov [ebx],eax;
   _asm mov [ebx+4],edx;
   return cycles;
}

This code will work on Win9X or NT/2K and probably XP. Actually, it would even work in Linux if you can figure out how to get GCC to emit inline assembler!

Of course, the time comes out in cycles, not seconds, and oddly there seems to be no API to get the machine's speed. You can either "calibrate" the results (by getting the count, sleeping for (say) one second, getting the count again, and doing some trivial math), or just use cycles directly and don't worry about seconds (it's great for comparing one algorithm to another, or finding the slow parts of your program)

One warning, however: certain machines, notably laptops, can slow down their processor speed when nothing important seems to be happening. Since the calibration sleep is exactly one of those times, you can get some seriously wrong results from the calibration.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralToo bad you didn't research more on this Pin
amir.tet
6:45 30 Aug '09  
QuestionInconsistancy with Vista. Pin
Jose Praveen
18:02 22 Oct '08  
AnswerRe: Inconsistancy with Vista. Pin
joshua0137
19:18 6 Jan '09  
GeneralDon't have C++ installed Pin
deletethisprofile
20:17 7 Aug '08  
GeneralC# Pin
screig
6:28 13 Jul '06  
GeneralPlease verify this ...... Pin
Anonymous
2:41 25 Mar '05  
GeneralRe: Please verify this ...... Pin
Anonymous
4:36 25 Mar '05  
Generaltimer out of this? Pin
nikoladsp
0:57 31 May '04  
GeneralWhat's the point Pin
Anonymous
2:10 12 Dec '03  
GeneralRe: What's the point Pin
nastyimp13
11:16 16 Nov '05  
GeneralRe: What's the point Pin
Robert Bielik
3:22 9 Jun '06  
GeneralQueryPerformanceFrequency Pin
parisitic
16:58 10 Dec '03  
AnswerRe: QueryPerformanceFrequency Pin
admiralh2
8:57 16 Mar '07  
Generalclobbering ebx Pin
LBMT
15:40 10 Dec '03  
GeneralRe: clobbering ebx Pin
dCp303
6:10 12 Dec '03  
GeneralRe: clobbering ebx Pin
LBMT
6:42 12 Dec '03  
GeneralHow about other Intel CPU's Pin
Mad_C
21:26 9 Dec '03  
GeneralRDTSC for Watcom C/C++ Pin
c2j2
21:25 9 Dec '03  
GeneralWill HT processor work? Pin
Vincent Leong77
20:04 9 Dec '03  
GeneralRe: Will HT processor work? Pin
c2j2
21:31 9 Dec '03  
GeneralRe: Will HT processor work? Pin
tmangan
2:58 10 Dec '03  
GeneralSomewhat disappointed Pin
ReorX
10:51 4 Dec '03  
GeneralRe: Somewhat disappointed Pin
noshbar
0:45 9 Dec '03  
GeneralCode to query CPU frequency Pin
Zoltan Csizmadia
9:54 4 Dec '03  
GeneralRe: Nicer format Pin
WREY
10:56 4 Dec '03  


Last Updated 2 Dec 2003 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009