Hi All,

Today I would like to talk about the Stopwatch class in C#.

One of the things I love most about my job is that I can learn new things every day, sometimes I can even learn two or three new things in the same day, I am absolutely not ashamed of saying that I know nothing, as computer science is a field that is changing every second. Therefore it is important from my point of view to keep us up to date.

What I am going to talk about today is not new, it has been in the .NET Framework since version 2.0 but the truth is that I have never had the necessity of learning it.

I was requested to do some performance checks in a Microsoft Dynamics CRM 2011 plugin, the logic of the plugin was quite simple but the amount of records coming from the integration system was huge. We needed to be sure that this plugin was not affecting the performance in a really bad way, so we decided to measure the integration with and without the plugin.

My first idea was to measure the time using the following:

That is the most basic approach, do not tell me that was not the first idea you had when you first try to do the same!!! 😉

But then I decided to be clever, now I see that I was being silly enough with my first approach, and perform a quick search with Google ( Bing or whatever), luckily I found this:



In this two articles I learned that I what I thought was the best solution was completely wrong and in the second one I learned how to make my measurements more accurate.

Here is an example of how to use the Stopwatch class in C#, as you can see it is extremely easy:

Just remember to include the System.Diagnostics using.

For more information about the Stopwatch class, the MSDN will explain better than me 😉