Click here to Skip to main content
12,943,299 members (64,677 online)
Click here to Skip to main content
Add your own
alternative version


25 bookmarked
Posted 12 Dec 2001

High Resolution Elapsed Timer

, 12 Dec 2001
Rate this:
Please Sign up or sign in to vote.
A simple high resolution timer class to help time code blocks.


The motivation for this came when I needed to time sections of code while analyzing performance issues. The existing timers did not provide the necessary granularity to accurately measure execution times in microseconds. So I ended up creating this simple class to do the timing.

Here's the class declaration:

// HighResElapsedTimer.h: interface for the HighResElapsedTimer class.


#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

// File:  HighResElapsedTimer.h
// Author:  Chen Venkataraman (
// Date:  Wed Dec 6, 2001
// Purpose: 
//  For determing elapsed time details during execution of code blocks 
//  (initially for debug builds only)
//  Sample usage:
//  {
//    #ifdef _DEBUG
//     HighResElapsedTimer  elapsedTime("block id");
//    #endif
//    ... rest of the code block
//  }
//    At the end of the above block, trace msg of the form
//    "block id : Elapsed time - microseconds" is spit out

#ifdef _DEBUG

class HighResElapsedTimer  
  HighResElapsedTimer(const CString& strName) : m_strName(strName)
    if (m_llFrequency == 0)      // frequency value not yet set
      LARGE_INTEGER  liFrequency;

      m_llFrequency = liFrequency.QuadPart;


  virtual ~HighResElapsedTimer()
    LARGE_INTEGER    liNow;
    double duration = (double)
      (liNow.QuadPart - m_llCounter.QuadPart)/m_llFrequency;

    TRACE(_T("%s : Elapsed time = %.3lf microseconds\n"), 
                                      m_strName, duration);

  CString      m_strName;
  LARGE_INTEGER  m_llCounter;

  static LONGLONG  m_llFrequency; 
  // Frequency setting is based hardware clock which doesn't
  // does change - so we want to set this only once

  HighResElapsedTimer(const HighResElapsedTimer&);
  HighResElapsedTimer& operator=(const HighResElapsedTimer&);

#endif  // _DEBUG


... and here is the implementation.

// HighResElapsedTime.cpp: implementation of the HighResElapsedTime class.



#include "stdafx.h"
#include "HighResElapsedTimer.h"

#ifdef _DEBUG

/* static */
LONGLONG HighResElapsedTimer::m_llFrequency = 0;

#endif    // _DEBUG

Since I was timing the code execution only during debug builds, I've guarded the code using #ifdef _DEBUG's & used TRACE for output. If you want to time release builds or output differently, it should be fairly trivial to do so.

I've tested this only on NT4 SP6a & hopefully it will work on Win95 m/cs also.


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

Chen Venkataraman
Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Generalseconds, not microseconds Pin
Anonymous5-Jun-02 6:58
memberAnonymous5-Jun-02 6:58 
GeneralOther timers Pin
Dean Wyant18-Dec-01 4:37
memberDean Wyant18-Dec-01 4:37 
GeneralRe: Other timers Pin
Rick York18-Dec-01 10:06
memberRick York18-Dec-01 10:06 
GeneralRe: Other timers Pin
Dean Wyant18-Dec-01 15:24
memberDean Wyant18-Dec-01 15:24 
GeneralRe: Other timers Pin
Rick York18-Dec-01 17:23
memberRick York18-Dec-01 17:23 
GeneralRe: Other timers Pin
Dean Wyant19-Dec-01 5:04
memberDean Wyant19-Dec-01 5:04 
GeneralRe: Other timers Pin
Crispy20-Dec-01 14:17
memberCrispy20-Dec-01 14:17 
QuestionWhat does QueryPerformanceCounter do? Pin
moliate14-Dec-01 0:02
membermoliate14-Dec-01 0:02 
AnswerRe: What does QueryPerformanceCounter do? Pin
Daniel Lohmann14-Dec-01 0:49
memberDaniel Lohmann14-Dec-01 0:49 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170518.1 | Last Updated 13 Dec 2001
Article Copyright 2001 by Chen Venkataraman
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid