Measure elapsed time with normal or high precision in C#

For most tasks, you can use the DateTime class's Now method to find out the current date and time. You can record the time before and after some event in variables start_time and stop_time. Then you can use stop_time.Subtract(start_time) to store the elapsed time in a TimeSpan variable. You can use that variable's properties to see how much time passed. For example, the TotalSeconds property returns the elapsed time in seconds as a double.

DateTime.Now is easy to use and fine for most purposes but for higher precision you can use the System.Diagnostics.Stopwatch class. This class can measure the duration of one interval or the sum of several intervals.

To add up several intervals, create a new Stopwatch object. Call its Start method to start the watch running and call Stop to stop it. You can start and stop the Stopwatch several times and it keeps a running total of elapsed time.

To measure a single interval, use the Stopwatch class's StartNew method to make a new Stopwatch object that is initialized and running. Use its Stop method to stop the watch.

After you've stopped a Stopwatch, use its Elapsed property to see how much time has passed. Elapsed returns a TimeSpan object so, for example, you can use its TotalSeconds property to see how many seconds have elapsed.

The following code shows how this example works.

private DateTime start_time, stop_time;
Stopwatch stop_watch;
Stopwatch total_watch = new Stopwatch();

private void Form1_Load(object sender, EventArgs e)
{
txtFrequency.Text = Stopwatch.Frequency.ToString();
txtNsPerTick.Text = (1000000000 / Stopwatch.Frequency).ToString();
txtIsHighRes.Text = Stopwatch.IsHighResolution.ToString();
}

private void btnStart_Click(object sender, EventArgs e)
{
btnStart.Enabled = false;
btnStop.Enabled = true;
txtElapsed1.Clear();
txtElapsed2.Clear();
txtElapsed3.Clear();

stop_watch = Stopwatch.StartNew();
total_watch.Start();
start_time = DateTime.Now;
}

private void btnStop_Click(object sender, EventArgs e)
{
stop_time = DateTime.Now;
stop_watch.Stop();
total_watch.Stop();

TimeSpan elapsed = stop_time.Subtract(start_time);
txtElapsed1.Text = elapsed.TotalSeconds.ToString("0.000000");

txtElapsed2.Text = stop_watch.Elapsed.TotalSeconds.ToString("0.000000");

txtElapsed3.Text = total_watch.Elapsed.TotalSeconds.ToString("0.000000");

btnStart.Enabled = true;
btnStop.Enabled = false;
}

When the program starts, it displays the Stopwatch's frequency, the number of nanoseconds per tick, and an indication of whether the class is using a high-performance timer. This last may return False on an older computer.

When you click the Start button, the program creates a new Stopwatch named stop_watch. It starts the Stopwatch named total_watch to accumulate multiple intervals. It also records DateTime.Now in the variable start_time.

When you click the Stop button, the program records DateTime.Now in the variable stop_time and stops both of the Stopwatches. It then displays the elapsed time recorded by the three methods.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.