Click here to Skip to main content
15,902,198 members
Articles / Programming Languages / C++
Article

How to get timings as fine-grained as one nanosecond or better

Rate me:
Please Sign up or sign in to vote.
4.53/5 (37 votes)
2 Dec 20031 min read 181.5K   41   45
How to get timings as fine-grained as one nanosecond or better

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralToo bad you didn't research more on this Pin
amir.tet30-Aug-09 5:45
amir.tet30-Aug-09 5:45 
QuestionInconsistancy with Vista. Pin
Jose Praveen22-Oct-08 17:02
Jose Praveen22-Oct-08 17:02 
AnswerRe: Inconsistancy with Vista. Pin
joshua01376-Jan-09 18:18
joshua01376-Jan-09 18:18 
GeneralDon't have C++ installed Pin
deletethisprofile7-Aug-08 19:17
deletethisprofile7-Aug-08 19:17 
GeneralC# Pin
screig13-Jul-06 5:28
screig13-Jul-06 5:28 
GeneralPlease verify this ...... Pin
Anonymous25-Mar-05 1:41
Anonymous25-Mar-05 1:41 
GeneralRe: Please verify this ...... Pin
Anonymous25-Mar-05 3:36
Anonymous25-Mar-05 3:36 
Questiontimer out of this? Pin
nikoladsp30-May-04 23:57
nikoladsp30-May-04 23:57 
GeneralWhat's the point Pin
Anonymous12-Dec-03 1:10
Anonymous12-Dec-03 1:10 
GeneralRe: What's the point Pin
nastyimp1316-Nov-05 10:16
nastyimp1316-Nov-05 10:16 
GeneralRe: What's the point Pin
Robert Bielik9-Jun-06 2:22
Robert Bielik9-Jun-06 2:22 
GeneralQueryPerformanceFrequency Pin
parisitic10-Dec-03 15:58
parisitic10-Dec-03 15:58 
AnswerRe: QueryPerformanceFrequency Pin
admiralh216-Mar-07 7:57
admiralh216-Mar-07 7:57 
Generalclobbering ebx Pin
LBMT10-Dec-03 14:40
LBMT10-Dec-03 14:40 
GeneralRe: clobbering ebx Pin
dCp30312-Dec-03 5:10
dCp30312-Dec-03 5:10 
GeneralRe: clobbering ebx Pin
LBMT12-Dec-03 5:42
LBMT12-Dec-03 5:42 
QuestionHow about other Intel CPU's Pin
Mad_C9-Dec-03 20:26
Mad_C9-Dec-03 20:26 
GeneralRDTSC for Watcom C/C++ Pin
c2j29-Dec-03 20:25
c2j29-Dec-03 20:25 
QuestionWill HT processor work? Pin
Vincent Leong779-Dec-03 19:04
Vincent Leong779-Dec-03 19:04 
AnswerRe: Will HT processor work? Pin
c2j29-Dec-03 20:31
c2j29-Dec-03 20:31 
AnswerRe: Will HT processor work? Pin
tmangan10-Dec-03 1:58
tmangan10-Dec-03 1:58 
You'll have to check the Intel doc on that (posted elsewhere against this article) - but without looking I would guess that it would not work well. I am guessing that it is part of the logical processor core -- which means that you are counting cycles at that level. If that is the case, the HT design is such that logical processors steal cycles from it's mate if the mate doesn't need them. If it is part of the global core, then you are OK on that, but now you are counting the sum of cycles used by each logical processor which is another issue.

Certainly, for both an HT situation, or any Multi-CPU situation, you will have to use processor affinity set to a single CPU. This ensures that the first and second measurements are run on the same (logical) processor, so that you can do the math.

Also note that this method will only work with measuring small portions of code (at best). And you need to run such a test many times -- picking the lowest result as the time used by your code. This is because the OS is multitasking and may well bump your process/thread to do some work on another thread (or interrupt). If the code being measured is small, does nothing to cause a wait (including disk, socket, or screen I/O), the system isn't very busy, and you run it enough times you will find that magic moment when the system dedicated the CPU to your thread the entire time. Applying processor affinity to other tasks to not use this CPU will also help your cause (although you cannot do that to certain system tasks).

So, while maybe not the world's best timer, it is a different tool for the bag.

tim
Founder, TMurgent Technologies
www.tmurgent.com
tmangan@tmurgent.com
GeneralSomewhat disappointed Pin
ReorX4-Dec-03 9:51
ReorX4-Dec-03 9:51 
GeneralRe: Somewhat disappointed Pin
noshbar8-Dec-03 23:45
noshbar8-Dec-03 23:45 
GeneralCode to query CPU frequency Pin
Zoltan Csizmadia4-Dec-03 8:54
Zoltan Csizmadia4-Dec-03 8:54 
GeneralRe: Nicer format Pin
WREY4-Dec-03 9:56
WREY4-Dec-03 9:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.