Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Simple Way to Measure Execution Time by Stopwatch

0.00/5 (No votes)
23 Sep 2020 1  
Easy way to measure execution time of the code block

Introduction

There are plenty of articles on how to measure execution time by stopwatch. Most of them are very complex. I will try to introduce my way to do it. I think that it will be the shortest one because like all programmers, I am lazy. ;)

I was frustrated when I tried to measure execution time of some functions. Problem was that every time I did try to do it, it was always the same code and it was always copy and paste. It's fine until you look at your code and on one page, you can see plenty of this:

stopwatch.Start();
SpecialFunctionToMeasure();
stopwatch.Stop()

And the problem was if your function triggers an exception which you didn't expect - like always when you think that everything is ok. :) If this happens, you will try to make code like this:

stopwatch.Start();
try
{
    SpecialFunctionToMeasure();
}
catch(....)
{
    ....
}
finally
{
    stopwatch.Stop();
}

It looks horrible. :) Isn't it ?

Solution

To understand my way of measuring time of execution, you have to have basic knowledge about C#, but I will try to explain in an easy way those few lines of code. My code for the class is as given below:

public class TimeMeasurementBlock : IDisposable
{
    public Stopwatch Stopwatch { get; private set; }

    public TimeMeasurementBlock(Stopwatch stopwatch)
    {
        Stopwatch = stopwatch;
        if (Stopwatch != null)
            Stopwatch.Start();
    }

    #region Implementation of IDisposable

    public void Dispose()
    {
        if (Stopwatch != null)
            Stopwatch.Stop();
    }

    #endregion
}

As you can see, the code is simple and should not disturb measurements. Of course, every additional code will disturb measurements, but I hope we are not making code for HFT. :)

You can skip public property if you wish. I put it because I am using it inside my code and it looks nice. :) Also, you can skip part with checking if Stopwatch is null if you are sure of what you are doing and to save few ticks. :)

Updated code to use new functionality of the C#:

public class TimeMeasurementBlock : IDisposable
{
    public Stopwatch Stopwatch { get; private set; }

    public TimeMeasurementBlock(Stopwatch stopwatch)
    {
        Stopwatch = stopwatch;
        Stopwatch?.Start();
    }


    #region Implementation of IDisposable

    public void Dispose()
    {
        Stopwatch?.Stop();
    }

    #endregion
}

Using the Code

It is very simple to use this class in your code. The code below should put light in this dark region... :)

Stopwatch stopwatch = new Stopwatch();
using (var timeBlock = new TimeMeasurementBlock(stopwatch))
{
...
}

...or if you don't need to access class inside block:

Stopwatch stopwatch = new Stopwatch();
using (new TimeMeasurementBlock(stopwatch))
{
...
}

As you probably know, "using" guarantees you that will call Dispose function at the end of the block even if will be exception inside. I think that this is the simplest solution for measuring execution time. Life is already very complicated so we don't have to complicate our source code. :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here