High Performance Timer

Below is a sample class containing a high performance timer. Example usuage:

PerformanceTimer pf = new PerformanceTimer(true);

float timeMs = pf.GetTimeMs();

using System;
using System.Runtime.InteropServices;

///
/// This class can be used to gather accurate performance timings.
///
public class PerformanceTimer
{


[DllImport("kernel32.dll")]
internal static extern bool QueryPerformanceCounter(ref long lpPerformanceCount);



[DllImport("kernel32.dll")]
internal static extern bool QueryPerformanceFrequency(ref long lpFrequency);



#region Private Variables

private bool _isRunning = false;
private long _startTime = 0;
private long _frequency = 0;
private float _elapsed = 0;

#endregion Private Variables



#region Constructors



///
/// Default constructor - does not start timer immediately.
///
public PerformanceTimer()
{

}



///
/// Constructor with option to start timer at same time.
///
///Whether to start timer immediately.

public PerformanceTimer(bool startTimer)

{

    if (startTimer)
    {

        Start();

    }

}

#endregion Constructors


#region Public Properties

///
/// Returns True if the timer is currently running.
///
/// To change the running state use the Start and Stop methods provided.

public bool IsRunning
{
    get { return _isRunning; }
}



#endregion Public Properties



#region Public Methods



///
/// Starts the performance timer running.
///

public void Start()

{
    QueryPerformanceFrequency(ref _frequency);
    QueryPerformanceCounter(ref _startTime);
    _elapsed = 0;
    _isRunning = true;

}



///
/// Stops the performance timer running.
///

public void Stop()

{

    // Call procedure that will set internal elapsed variable, then turn running flag off.

    GetTime();

    _isRunning = false;

}



///

/// Returns the time elapsed since "Start" was called (in seconds). If the timer

/// is not running the time elapsed between the last start and stop time is returned.

///

/// A float representing the time ellapsed (in seconds)

public float GetTime()

{

    if (_isRunning)
    {
        long endTime = 0;
        QueryPerformanceCounter(ref endTime);
        _elapsed = (float)(endTime - _startTime) / _frequency;
        return _elapsed;
    }
    else
    {
        return _elapsed;
    }
}



///
/// Returns the time elapsed since "Start" was called (in milliseconds). If the timer
/// is not running the time elapsed between the last start and stop time is returned.
///
/// A float representing the time ellapsed (in milliseconds)

public float GetTimeMs()

{
    if (_isRunning)
    {
        return GetTime() * 1000;
    }
    else
    {
        return _elapsed * 1000;

    }

}



///
/// Override ToString to display time nicely formatted.
///
/// Returns the time formatted as a string.

public override string ToString()

{
    return String.Format("{0:0000.000}s ", GetTime());
}
#endregion Public Methods

}

Leave a Reply

Your email address will not be published. Required fields are marked *