Click here to Skip to main content
Click here to Skip to main content
Go to top

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

, 2 Dec 2003
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Mark VanTassel
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralToo bad you didn't research more on this Pinmemberamir.tet30-Aug-09 5:45 
QuestionInconsistancy with Vista. PinmemberJose Praveen22-Oct-08 17:02 
AnswerRe: Inconsistancy with Vista. Pinmemberjoshua01376-Jan-09 18:18 
GeneralDon't have C++ installed Pinmemberdeletethisprofile7-Aug-08 19:17 
GeneralC# Pinmemberscreig13-Jul-06 5:28 
GeneralPlease verify this ...... PinsussAnonymous25-Mar-05 1:41 
GeneralRe: Please verify this ...... PinsussAnonymous25-Mar-05 3:36 
Questiontimer out of this? Pinmembernikoladsp30-May-04 23:57 
GeneralWhat's the point PinsussAnonymous12-Dec-03 1:10 
GeneralRe: What's the point Pinmembernastyimp1316-Nov-05 10:16 
GeneralRe: What's the point PinmemberRobert Bielik9-Jun-06 2:22 
GeneralQueryPerformanceFrequency Pinmemberparisitic10-Dec-03 15:58 
AnswerRe: QueryPerformanceFrequency Pinmemberadmiralh216-Mar-07 7:57 
Generalclobbering ebx PinmemberLBMT10-Dec-03 14:40 
GeneralRe: clobbering ebx PinmemberdCp30312-Dec-03 5:10 
GeneralRe: clobbering ebx PinmemberLBMT12-Dec-03 5:42 
QuestionHow about other Intel CPU's PinmemberMad_C9-Dec-03 20:26 
GeneralRDTSC for Watcom C/C++ Pinmemberc2j29-Dec-03 20:25 
QuestionWill HT processor work? PinmemberVincent Leong779-Dec-03 19:04 
AnswerRe: Will HT processor work? Pinmemberc2j29-Dec-03 20:31 
AnswerRe: Will HT processor work? Pinmembertmangan10-Dec-03 1:58 
GeneralSomewhat disappointed PinmemberReorX4-Dec-03 9:51 
GeneralRe: Somewhat disappointed Pinmembernoshbar8-Dec-03 23:45 
GeneralCode to query CPU frequency PinmemberZoltan Csizmadia4-Dec-03 8:54 
GeneralRe: Nicer format PinmemberWREY4-Dec-03 9:56 
GeneralRe: Nicer format Pinmemberthought4-Dec-03 21:45 
GeneralRe: Nicer format PinmemberDarka25-Jul-06 23:31 
GeneralRe: Nicer format PinmemberDarka25-Jul-06 23:32 
JokeRe: Nicer format PinmemberMars10516-May-07 3:02 
JokeRe: Nicer format PinmemberMars10516-May-07 3:04 
GeneralRe: Nicer format PinmemberAjay Vijayvargiya16-Aug-10 19:18 
GeneralRe: Nicer format PinmemberWREY5-Dec-03 10:23 
GeneralRe: Nicer format PinmemberDavid Pritchard9-Dec-03 9:56 
GeneralMinor change PinmemberZoltan Csizmadia4-Dec-03 6:47 
GeneralRe: Minor change PinmemberRobert Bielik4-Dec-03 20:41 
GeneralOne note about rdtsc Pinmembermtseitlin3-Dec-03 23:38 
GeneralRe: One note about rdtsc PinmemberRobert Bielik4-Dec-03 1:42 
GeneralRe: One note about rdtsc PinsussMaxim Tseitlin4-Dec-03 4:41 
GeneralRe: One note about rdtsc PinmemberJohn M. Drescher4-Dec-03 5:25 
GeneralRe: One note about rdtsc PinmemberRobert Bielik4-Dec-03 20:10 
GeneralRe: One note about rdtsc Pinmemberpeterchen8-Dec-03 21:19 
GeneralGood Job Pinmemberhcg3-Dec-03 20:09 
GeneralNow this looks interesting... PinmemberColin Angus Mackay3-Dec-03 16:44 
GeneralRe: Now this looks interesting... PinmemberJohn M. Drescher3-Dec-03 16:59 

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.

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