Monday

Stopwatch class for performance testing in C#

Stopwatch is a very handy class if you want to test your code’s performance. It is found in System.Diagnostics namespace. Stopwatch instance can be used to measure elapsed time between one interval and total of elapsed time across multiple intervals. This class provides either high-resolution performance counter or performance counter based on DateTime class, which is less accurate. It selects the performance counter based on your system.

If you have stumbled over this article, then the chances are that you might be in a rush to learn how to use this class in your application.

Let’s Start:
using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;

namespace StopWatchTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            Stopwatch watch = new Stopwatch();
            
            //Starting stopwatch
            watch.Start();

            for (int i = 0; i < 10; i++)
            {
                Thread.Sleep(1000);
            }
            //Pausing stopwatch
            watch.Stop();

            //Resuming stopwatch
            watch.Start();

            for (int i = 0; i < 10; i++)
            {
                Thread.Sleep(100);
            }

            watch.Stop();

            //Cumulative result
            MessageBox.Show
            (
                string.Format("Time spent in {0} Ticks", 
                   watch.ElapsedTicks.ToString()) + Environment.NewLine +
                string.Format("Time spent in {0} Milliseconds", 
                  watch.ElapsedMilliseconds.ToString()) + Environment.NewLine +
                string.Format("Time spent in {0} Seconds", 
                 (watch.ElapsedMilliseconds / 1000).ToString())
            );
        }

        private void btnTest2_Click(object sender, EventArgs e)
        {
            Stopwatch watch = new Stopwatch();

            //Starting Stopwatch
            watch.Start();

            for (int i = 0; i < 10; i++)
            {
                Thread.Sleep(10);
            }

            //Pausing Stopwatch
            watch.Stop();

            //Resuming stopwatch
            watch.Start();

            for (int i = 0; i < 10; i++)
            {
                Thread.Sleep(100);
            }
            //Pausing Stopwatch again
            watch.Stop();
            
            //Getting the elapsed time as a TimeSpan value
            TimeSpan span = watch.Elapsed;

            //Getting timings for recent execution
            MessageBox.Show
            (
                string.Format
                (
                    "Time spend in {0} Hours, {1} Minutes, {2} Seconds, {3} Milliseconds", 
                    span.Hours.ToString(), 
                    span.Minutes.ToString(), 
                    span.Seconds.ToString(), 
                    span.Milliseconds.ToString()
                )
            );

            //Getting timings for total execution
            MessageBox.Show
            (
                string.Format
                (
                    "Total time spend in {0} Hours, {1} Minutes, {2} Seconds, {3} Milliseconds",
                    span.TotalHours.ToString(),
                    span.TotalMinutes.ToString(),
                    span.TotalSeconds.ToString(),
                    span.TotalMilliseconds.ToString()
                )
            );
        }

        private void btnTest3_Click(object sender, EventArgs e)
        {
            Stopwatch watch = new Stopwatch();
            if (Stopwatch.IsHighResolution)
            {
                MessageBox.Show
                (
                    "Stopwatch is using high resolution performance counter"
                );
            }
            else
            {
                MessageBox.Show
                (
                    "Stopwatch is using DateTime class"
                );
            }

            MessageBox.Show
            (
                string.Format
                (
                    "Current ticks per second frequency: {0}", 
                    Stopwatch.Frequency.ToString()
                )
            );

            MessageBox.Show
            (
                string.Format
                (
                    "Nanoseconds per tick: {0}", 
                    ((1000L*1000L*1000L) / Stopwatch.Frequency).ToString()
                )
            );
        }
    }
}


If you are interested in learning Stopwatch class in depth then check out Stopwatch class explained at MSDN: http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx

No comments:

Post a Comment

Your comments are highly appreciated!