C# Sample: Timing Class
Posted on Sunday, January 25, 2009
This timing class can be used to test the execution time of your C# algorithms. We used it to compare the time it took various sort and search functions to complete execution.
Timing.cs
Here's the code:
class Timing
{
DateTime startTime;
TimeSpan duration;
/// <summary>
/// Default constructor. Sets start time and duration to zero.
/// </summary>
public Timing()
{
startTime = System.DateTime.Now;
duration = new TimeSpan( 0 );
}
/// <summary>
/// Overloaded constructor. Accepts an initial start time as a parameter. Sets duration to zero.
/// </summary>
/// <param name="start">Accepts a TimeSpan starting value.</param>
public Timing( DateTime start )
{
this.startTime = start;
this.duration = new TimeSpan( 0 );
}
/// <summary>
/// Overloaded constructor. Accepts an initial start time and duration as parameters.
/// </summary>
/// <param name="start">Accepts a TimeSpan starting value.</param>
/// <param name="end">Accepts a TimeSpan elapsed time value.</param>
public Timing( DateTime start, TimeSpan elapsedtime )
{
this.startTime = start;
this.duration = elapsedtime;
}
/// <summary>
/// Method to calculate the duration of the timed event.
/// </summary>
public void StopTime()
{
this.duration = System.DateTime.Now.Subtract( startTime );
}
/// <summary>
/// Method to set the timed event's start time.
/// </summary>
public void StartTime()
{
GC.Collect();
GC.WaitForPendingFinalizers();
startTime = System.DateTime.Now;//Process.GetCurrentProcess().Threads[0].UserProcessorTime;
}
/// <summary>
/// Method to extract the timed event's duration.
/// </summary>
/// <returns>The time, in milliseconds, required for the timed event to execute.</returns>
public TimeSpan Result()
{
return duration;
}
}// end class