MS Tech & Coffee

Thoughts of a .NET Developer turned into MS Dynamics architect turned into Power Platform Director turned into AI and Business Transformation Global Lead… all views are my own!

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:

http://www.dijksterhuis.org/timing-function-performance-stopwatch-class

http://www.codeproject.com/Articles/61964/Performance-Tests-Precise-Run-Time-Measurements-wi

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 😉

http://msdn.microsoft.com/en-us/library/vstudio/system.diagnostics.stopwatch(v=vs.90).aspx

Cheers,

Mc

One response to “StopWatch Class C#, the way of measure elapsed time”

  1. lista de email Avatar

    this blog is very informative. i am really impressed by the comments which people have given over here. i am sure many people will get more and more knowledge from this. lista de email lista de email lista de email lista de email lista de email

Leave a comment

I’m Mario

Welcome to MS Tech & Coffee, a project I started more than 8 years ago as a place where I could drop things that I was encountering in my journey as .NET developer in the UK. That journey has taken me to many places and now looking back I want to use this project to share some of my personal views on technology, AI, the Microsoft ecosystem and of course.. a bit of coffee!

Let’s connect