By Eric Johnson
Download the Code for my Free C#.NET StopWatch here.
After searching through code insight and the Internet. I came to the conclusion
that there either wasn't a good execution timer to use or that it was very hard
to find. Before I dicuss my solution, here are some of the other solutions that
I found during my research.
I found a very accurate StopWatch class written in C# at http://www.mentalis.org/soft/classes.qpx.
It looked like it would do the job but was a little much for my needs.
You could also wrap the System.Timers.Timer class inside a StopWatch class but the
timer class is event driven. That means that if you want to track in milliseconds
you will have to raise an event at an interval of 1 millisecond, which would raise
a lot of events.
My firt part of my Solution was to use the the DateTime.Now Collection and simple
math to calculate the elasped time. Here is the class that I wrote:
using System;
namespace DynamicWebDesign
{
///
/// Accurate, Simple, and Easy to use Stopwatch Class. This class
/// can be used to track process execution time. I typically write
/// the Elasped time to the trace log if I'm writing a ASP.net
/// application.
///
/// Sample Usage:
/// StopWatch sw = new StopWatch();
/// sw.Start;
/// // Do Process1
/// Trace.Write("Stopwatch", "Process1:" sw.GetTime());
/// // Do Process2
/// Trace.Write("Stopwatch", "Process2:" sw.GetTime());
/// sw.Stop
/// // Do Process3
/// Trace.Write("Stopwatch", "Process 1 & 2:" sw.GetTime());
///
public class StopWatch
{
private int _StartTime;
private int _StopTime;
private int StartTime
{
get
{
return this._StartTime;
}
set
{
this._StartTime = value;
}
}
private int StopTime
{
get
{
return this._StopTime;
}
set
{
this._StopTime = value;
}
}
///
/// Initializes the StopWatch to 0.
///
public StopWatch()
{
StartTime = 0;
StopTime = 0;
}
///
/// Starts the Stopwatch.
///
public void Start()
{
StartTime =
DateTime.Now.Hour * 60 * 60 * 1000 +
DateTime.Now.Minute * 60 * 1000 +
DateTime.Now.Second * 1000 +
DateTime.Now.Millisecond;
//Console.WriteLine("StartTime: " + StartTime);
}
///
/// Stops the StopWatch.
///
public void Stop()
{
StopTime =
DateTime.Now.Hour * 60 * 60 * 1000 +
DateTime.Now.Minute * 60 * 1000 +
DateTime.Now.Second * 1000 +
DateTime.Now.Millisecond;
//Console.WriteLine("StopTime: " + StopTime);
}
///
/// Resets the Stopwatch to 0.
///
public void Reset()
{
StartTime = DateTime.Now.Millisecond;
StopTime = DateTime.Now.Millisecond;
}
///
/// Returns a string containing the elasped time since the Start
/// of the StopWatch.
///
/// (If Called after the Stop Method)
/// Returns a string containing the elasped time between the Start
/// of the StopWatch and the Stop of the StopWatch
///
public string GetTime()
{
int CurrentTime;
float Elasped;
CurrentTime =
DateTime.Now.Hour * 60 * 60 * 1000 +
DateTime.Now.Minute * 60 * 1000 +
DateTime.Now.Second * 1000 +
DateTime.Now.Millisecond;
if(StopTime == 0)
Elasped = (CurrentTime - StartTime)/(float)1000;
else
Elasped = (StopTime - StartTime)/(float)1000;
return Elasped.ToString();
}
}
}
The second part of my solution was to write the value
that GetTime returned into the trace log. This way I could profile the application
if I compiled in debug mode and remove the profiling by compiling into release mode.