Click here to Skip to main content
6,915,647 members and growing! (15,826 online)
Email Password   helpLost your password?
General Programming » Date and Time » General     Intermediate

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

By Mark VanTassel

How to get timings as fine-grained as one nanosecond or better
VC6, VC7, VC7.1Win2K, WinXP, Win2003, Visual-Studio, Dev
Posted:2 Dec 2003
Views:107,149
Bookmarked:37 times
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
37 votes for this article.
Popularity: 6.91 Rating: 4.41 out of 5
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

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


Member

Occupation: Web Developer
Location: United States United States

Other popular Date and Time articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 44 (Total in Forum: 44) (Refresh)FirstPrevNext
GeneralToo bad you didn't research more on this Pinmemberamir.tet6:45 30 Aug '09  
QuestionInconsistancy with Vista. PinmemberJose Praveen18:02 22 Oct '08  
Confused
Dear all,

Any of you have tested this code under Vista.?It is working perfectly under all Windows OS (incl XP) with acceptable consistancy (less than 0.2% error) with resolutions upto 0.0001 seconds.

But when attepted on Vista, its not providing that sort of accuracy, even after disabling all eye-candy graphic options of Vista.

Any suggestions on this or support on a code for a consistant-high-resolution timer on Vista would is greatly appreciated.
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  
Generaltimer 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  
GeneralHow about other Intel CPU's PinmemberMad_C21:26 9 Dec '03  
GeneralRDTSC for Watcom C/C++ Pinmemberc2j221:25 9 Dec '03  
GeneralWill HT processor work? PinmemberVincent Leong7720:04 9 Dec '03  
GeneralRe: Will HT processor work? Pinmemberc2j221:31 9 Dec '03  
GeneralRe: Will HT processor work? Pinmembertmangan2:58 10 Dec '03  
GeneralSomewhat disappointed PinmemberReorX10:51 4 Dec '03  
GeneralRe: Somewhat disappointed Pinmembernoshbar0:45 9 Dec '03  
GeneralCode to query CPU frequency PinmemberZoltan Csizmadia9:54 4 Dec '03  
GeneralRe: Nicer format PinmemberWREY10:56 4 Dec '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 2 Dec 2003
Editor: Nishant Sivakumar
Copyright 2003 by Mark VanTassel
Everything else Copyright © CodeProject, 1999-2010
Web20 | Advertise on the Code Project