1. Computing

How to Accurately Measure Elapsed Time Using High-Resolution Performance Counter

The TStopWatch Delphi Class Implements a Very Accurate Process Execution Timer


tstopwatch delphi high performance timer

tstopwatch delphi high performance timer

In most "normal" database applications, for example, it is not that much important if some piece of code would take 1 or 2 seconds to execute.

What about when you need to process millions of tree leafs or generate zillions of unique random numbers? In such scenarios it is important that your code executes as much fast as possible.

Timing Out Your Code

In some applications, a very accurate, high-precision time measurement methods are important.

Using RTL's Now
The first idea that comes to (my) mind is simply using the Now function. Now, defined in the SysUtils unit, returns the current system date and time.

A few lines of code can be used to measure elapsed time between "start" and "stop" of some process:

   start, stop, elapsed : TDateTime;
   start := Now;
   stop := Now;
   elapsed := stop - start;
Now function returns the current system date and time that is accurate up to 10 milliseconds (Windows NT and later) or 55 milliseconds (Windows 98).

For very small intervals the precision of "Now" is sometimes not enough.

Using Windows API GetTickCount
The second idea that comes to mind is using the GetTickCount Windows API function. GetTickCount retrieves the number of milliseconds that have elapsed since the system was started, but the function only has the precision of 1 ms and is not very accurate.

The elapsed time is stored as a DWORD (32-bit) value. Therefore, the time will wrap around to zero if Windows is run continuously for 49.7 days.

   start, stop, elapsed : cardinal;
   start := GetTickCount;
   stop := GetTickCount;
   elapsed := stop - start; //milliseconds

GetTickCount is also limited to the accuracy of the system timer (10 / 55 ms).

High Precision Timing Out Your Code

Finally, if a high-resolution performance counter exists on the system, you can use the QueryPerformanceFrequency Windows API function to express the frequency, in counts per second. The value of the count is processor dependent.

QueryPerformanceFrequency returns the number of "ticks" per seconds - it is the amount that the counter, QueryPerformanceCounter, will increment in a second.

The QueryPerformanceCounter function retrieves the current value of the high-resolution performance counter. By calling this function at the beginning and end of a section of code, an application uses the counter as a high-resolution timer.

The accuracy of a high-resolution timers is around few hundred nanoseconds. A nanosecond is a unit of time representing 0.000000001 or 1 billionth of a second!

TStopWatch - Delphi Implementation Of a High Resolution Counter

Here's the implementation of a custom high-resolution timer in Delphi: TStopWatch. I've borrowed the name from .Net :)

TStopWatch measures elapsed time by counting timer ticks in the underlying timer mechanism.

  • The IsHighResolution property indicates whether the timer is based on a high-resolution performance counter.
  • The Start method starts measuring elapsed time.
  • The Stop method stops measuring elapsed time.
  • The ElapsedMilliseconds property gets the total elapsed time in milliseconds.
  • The Elapsed property gets the total elapsed time in timer ticks.
 unit StopWatch;
 uses Windows, SysUtils, DateUtils;
 type TStopWatch = class
     fFrequency : TLargeInteger;
     fIsRunning: boolean;
     fIsHighResolution: boolean;
     fStartCount, fStopCount : TLargeInteger;
     procedure SetTickStamp(var lInt : TLargeInteger) ;
     function GetElapsedTicks: TLargeInteger;
     function GetElapsedMilliseconds: TLargeInteger;
     function GetElapsed: string;
     constructor Create(const startOnCreate : boolean = false) ;
     procedure Start;
     procedure Stop;
     property IsHighResolution : boolean read fIsHighResolution;
     property ElapsedTicks : TLargeInteger read GetElapsedTicks;
     property ElapsedMilliseconds : TLargeInteger read GetElapsedMilliseconds;
     property Elapsed : string read GetElapsed;
     property IsRunning : boolean read fIsRunning;
 constructor TStopWatch.Create(const startOnCreate : boolean = false) ;
   inherited Create;
   fIsRunning := false;
   fIsHighResolution := QueryPerformanceFrequency(fFrequency) ;
   if NOT fIsHighResolution then fFrequency := MSecsPerSec;
   if startOnCreate then Start;
 function TStopWatch.GetElapsedTicks: TLargeInteger;
   result := fStopCount - fStartCount;
 procedure TStopWatch.SetTickStamp(var lInt : TLargeInteger) ;
   if fIsHighResolution then
     lInt := MilliSecondOf(Now) ;
 function TStopWatch.GetElapsed: string;
   dt : TDateTime;
   dt := ElapsedMilliseconds / MSecsPerSec / SecsPerDay;
   result := Format('%d days, %s', [trunc(dt), FormatDateTime('hh:nn:ss.z', Frac(dt))]) ;
 function TStopWatch.GetElapsedMilliseconds: TLargeInteger;
   result := (MSecsPerSec * (fStopCount - fStartCount)) div fFrequency;
 procedure TStopWatch.Start;
   SetTickStamp(fStartCount) ;
   fIsRunning := true;
 procedure TStopWatch.Stop;
   SetTickStamp(fStopCount) ;
   fIsRunning := false;
Here's an example of usage:
   sw : TStopWatch;
   elapsedMilliseconds : cardinal;
   sw := TStopWatch.Create() ;
     elapsedMilliseconds := sw.ElapsedMilliseconds;

©2014 About.com. All rights reserved.