Click here to Skip to main content
15,900,906 members
Articles / Desktop Programming / MFC
Article

High Resolution Elapsed Timer

Rate me:
Please Sign up or sign in to vote.
3.00/5 (9 votes)
12 Dec 2001 102.6K   824   25   9
A simple high resolution timer class to help time code blocks.

Introduction

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 !defined(HIGHRESELAPSEDTIMER_H_INCLUDED_)
#define HIGHRESELAPSEDTIMER_H_INCLUDED_

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

// File:  HighResElapsedTimer.h
// Author:  Chen Venkataraman (venkatar@sig.com)
// 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 - xxxx.xxx microseconds" is spit out

#ifdef _DEBUG

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

      QueryPerformanceFrequency(&liFrequency);
      m_llFrequency = liFrequency.QuadPart;
    }

    QueryPerformanceCounter(&m_llCounter);
  }

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

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

private:
  CString      m_strName;
  LARGE_INTEGER  m_llCounter;

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

private:
  HighResElapsedTimer(const HighResElapsedTimer&);
  HighResElapsedTimer& operator=(const HighResElapsedTimer&);
};

#endif  // _DEBUG

#endif  // !defined(HIGHRESELAPSEDTIMER_H_INCLUDED_)

... 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.

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
Software Developer (Senior)
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

 
Generalseconds, not microseconds Pin
5-Jun-02 6:58
suss5-Jun-02 6:58 
GeneralOther timers Pin
Dean Wyant18-Dec-01 4:37
Dean Wyant18-Dec-01 4:37 
GeneralRe: Other timers Pin
Rick York18-Dec-01 10:06
mveRick York18-Dec-01 10:06 
GeneralRe: Other timers Pin
Dean Wyant18-Dec-01 15:24
Dean Wyant18-Dec-01 15:24 
Yes, there are other timers. PJ Naughter's is not based on QueryPerformanceCounter. CPerfTimer is. Since I am not aware of your exact usage and needs for this code, I am not saying CPerfTimer is more suited for what you are doing. Just that it was already available here and based on QueryPerformanceCounter like the small class in this article.

It could have been used simply like below. Since the class in this article is so simple, it would not have saved any time:

Use proper includes etc.

class CBlockTimer : public CPerfTimer
{
protected:
CString m_sName;

public:
CBlockTimer(LPCTSTR pszName);

virtual ~CBlockTimer();

};

CBlockTimer::CBlockTimer(LPCTSTR pszName) : CPerfTimer(TRUE)
{
m_sName = pszName;
}

CBlockTimer::CBlockTimer()
{
TRACE(_T("%s : Elapsed time = %.3lf microseconds\n"), m_sName, Elapsedms());
}

Usage would be the same, just use CBlockTimer instead of HighResElapsedTimer.

However, I would not recommend using it this way because TRACE only works in debug mode and timing operations like this in debug mode isn't very accurate with any timer. The problem is the debugger overhead and inconsistency in running the code. Each run can be sometimes be very different. The above is just an example of the same usage as the article has. For accurate timing and comparison of changes, release mode should be used and the destructor would need to use something besides TRACE. But, I would not do it that way either. I wrote a class based on CPerftimer that keeps running times on blocks sort of like this article, but the times are kept in an array in the class (static) and written to a file at the end of the run with counts, minimums, maximums and averages. The profiler class has an empty replacement wrapped in defines so it compiles to nothing when not needed. Before getting an advanced profiler, I used it in release mode. Unfortunately I have never had the time to clean it up and post it here. There are also other similar profiler classes available (I think). If not I may consider posting it. Gee, I added some delay functions long ago to CPerfTimer (on request) and have not posted the update for them either.

Since I use a transparent profiler now, I find that I use a timer class mostly as a static or member variable for performing periodic operations instead of using an OS timer. Also for keeping certain log messages etc. from being endlessly repeated by only allowing them to be repeated once a minute or so. Another use is for reducing the time necessary for getting the current time for stamping data records. Since repeatedly getting the current time is slow, I used CPerfTimer to keep time between periodic OS calls to get the time. A call to get the current time just adds the timer value unless the timer value is over a certain threshold, then the current time is retrieved using the OS call and the timer is reset. Much more efficient when possibly getting the current time many times a second. Not many programs need to do that though.

There are many uses for most timer classes, but not many for the one in this article. I am not trying to bash this article or its author, this class may suit the author's needs. I am just trying to be helpful to others who may have other needs or expectations (saving them time).

Smile | :)

<only the="" weak="" purposefully="" send="" viruses.="" strong="" are="" immune.="">
GeneralRe: Other timers Pin
Rick York18-Dec-01 17:23
mveRick York18-Dec-01 17:23 
GeneralRe: Other timers Pin
Dean Wyant19-Dec-01 5:04
Dean Wyant19-Dec-01 5:04 
GeneralRe: Other timers Pin
20-Dec-01 14:17
suss20-Dec-01 14:17 
QuestionWhat does QueryPerformanceCounter do? Pin
moliate14-Dec-01 0:02
moliate14-Dec-01 0:02 
AnswerRe: What does QueryPerformanceCounter do? Pin
Daniel Lohmann14-Dec-01 0:49
Daniel 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.