Click here to Skip to main content
Licence 
First Posted 2 Dec 2003
Views 129,155
Bookmarked 38 times

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

By Mark VanTassel | 2 Dec 2003
How to get timings as fine-grained as one nanosecond or better
2 votes, 5.4%
1
2 votes, 5.4%
2
3 votes, 8.1%
3
11 votes, 29.7%
4
19 votes, 51.4%
5
4.53/5 - 37 votes
4 removed
μ 4.41, σa 2.00 [?]

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

About the Author

Mark VanTassel

Web Developer

United States United States

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralToo bad you didn't research more on this Pinmemberamir.tet6:45 30 Aug '09  
QuestionInconsistancy with Vista. PinmemberJose Praveen18:02 22 Oct '08  
AnswerRe: Inconsistancy with Vista. Pinmemberjoshua013719:18 6 Jan '09  
GeneralDon't have C++ installed Pinmemberdeletethisprofile20:17 7 Aug '08  
GeneralC# Pinmemberscreig6:28 13 Jul '06  
GeneralPlease verify this ...... PinsussAnonymous2:41 25 Mar '05  
GeneralRe: Please verify this ...... PinsussAnonymous4:36 25 Mar '05  
Questiontimer out of this? Pinmembernikoladsp0:57 31 May '04  
GeneralWhat's the point PinsussAnonymous2:10 12 Dec '03  
GeneralRe: What's the point Pinmembernastyimp1311:16 16 Nov '05  
GeneralRe: What's the point PinmemberRobert Bielik3:22 9 Jun '06  
GeneralQueryPerformanceFrequency Pinmemberparisitic16:58 10 Dec '03  
AnswerRe: QueryPerformanceFrequency Pinmemberadmiralh28:57 16 Mar '07  
Generalclobbering ebx PinmemberLBMT15:40 10 Dec '03  
GeneralRe: clobbering ebx PinmemberdCp3036:10 12 Dec '03  
GeneralRe: clobbering ebx PinmemberLBMT6:42 12 Dec '03  
QuestionHow about other Intel CPU's PinmemberMad_C21:26 9 Dec '03  
GeneralRDTSC for Watcom C/C++ Pinmemberc2j221:25 9 Dec '03  
QuestionWill HT processor work? PinmemberVincent Leong7720:04 9 Dec '03  
AnswerRe: Will HT processor work? Pinmemberc2j221:31 9 Dec '03  
AnswerRe: Will HT processor work? Pinmembertmangan2:58 10 Dec '03  
GeneralSomewhat disappointed PinmemberReorX10:51 4 Dec '03  
GeneralRe: Somewhat disappointed Pinmembernoshbar0:45 9 Dec '03  
Ok, so if it only takes a few lines to explain, why not be the saving grace for allll CodeProject members and explain it? (I would, but I'm one of the stupid few who don't neeeed to understand why the transistor flops over before I'm prepared to use an instruction)
 
Secondly, how would you recommend doing nanosecond timing if this method is still so unpredictable and unstable?
 
AND, dude, seriously, calm down. If people didn't know about rdtsc before, they do now, so they could go look up on it.
 
AND I don't claim to understand how rdtsc works, but in _my_ tests, on Linux (which should excite you no end), on an AMD K6, it worked just fine. So I dunno how to explain that seeing as it's impossible, ahem, but -hey-, it works nicely.
 


Crayons don't kill people, death does.
GeneralCode to query CPU frequency PinmemberZoltan Csizmadia9:54 4 Dec '03  
GeneralRe: Nicer format PinmemberWREY10:56 4 Dec '03  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120210.1 | Last Updated 3 Dec 2003
Article Copyright 2003 by Mark VanTassel
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid