Measure elapsed time in C#

For most tasks, you can measure elapsed time with the Stopwatch or DateTime classes.

To use the Stopwatch class, create a Stopwatch object. Use its Start and Stop methods to start and stop it. If you use Start after stopping the object, the time picks up where it left off. That makes this class particularly useful if you want to add up several intervals. Use the Reset method to reset the time to 0. Use the StartNew method to create a new Stopwatch and start it running in a single step. Use the Stopwatch class’s Elapsed property to get a TimeSpan object representing the elapsed time.

To use the DateTime class, use the static Now property to get the current time before and after you execute some code. Save the results in DateTime variables. Then subtract them to get a TimeSpan representing the elapsed time.

When the program starts, it uses the following code to get ready to run and to display information about the Stopwatch class.

private DateTime StartTime, StopTime;
Stopwatch StopWatch;
Stopwatch TotalWatch = 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();
}

The code first declares two DateTimes and two Stopwatches. The form’s Load event handler displays the Stopwatch class’s frequency (ticks per second) value and the number of nanoseconds per tick. It also indicates whether the class uses high-resolution timing. This last may return false on an older computer.

When you click the Start button, the following code executes.

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

    StartTime = DateTime.Now;
    StopWatch = Stopwatch.StartNew();
    TotalWatch.Start();
}

This code disables the Start button and enables the Stop button. It then clears the result text boxes.

Next the code sets StartTime to the current time. It then creates and starts a new Stopwatch, saving it in the variable StopWatch. Finally it restarts the TotalWatch Stopwatch object.

When you click the Stop button, the following code executes.

private void btnStop_Click(object sender, EventArgs e)
{
    StopTime = DateTime.Now;
    StopWatch.Stop();
    TotalWatch.Stop();

    TimeSpan elapsed = StopTime.Subtract(StartTime);
    txtElapsed1.Text = elapsed.TotalSeconds.ToString("0.000000");

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

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

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

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

If you run the program several times, you’ll see that the DateTime and Stopwatch classes produce very close to the same results.






This entry was posted in performance, system and tagged , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

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