Stop Watch Code Testing Performance On Any Code Part 1



There has most likely been a time in your programming life (most likely many many many times), where you will think to yourself, “ Is what I’m doing here as efficient as doing it the 50 other ways to do it? Or should I try to improve it. 2 things should go off in your head.

First is, much like multiple choice questions on  an exam in College, if you are pretty unsure, chances are, it can be made more efficient. Our instincts are more often correct than not. That’s not to say that you have time to actually fix it, but if the alarm goes off in your head, if time permits, take a look at it.

Second alarm that should go off is, “What other options would in fact make this more efficient?” This is harder to answer right away, but Google being our companion in a programmers life, Usually what you are looking to do has been done at some point or another if you break down what you are trying to do. Now, this is not to say that what you are doing as a project has been done before, that’s half of why programmers exist, Customization! What I ‘m talking about is the break down of individual routines/methods that could be adversely effecting other code. Here is a example of not using the power of .NET:

/// <summary> /// Returns current UTC time /// </summary> /// <returns></returns> public static DateTime GetCurrentTimeUtc() { DateTime dt = DateTime.UtcNow; return dt; }



So what’s wrong here you say? Well, if you already have a static method in an object that exists in your framework, what is the point of making a new routine to accomplish the same task and at the same time creating 2 objects instead of 1 that the stack needs to worry about. In this example, is the performance minimal, sure, but what if this was done all over this persons code? And the main code calls it constantly? Then you are going to may start seeing the software degrade substantially.  So lets say we want to test this? What does one do? Lets make a StopWatch Tool. This isn’t going to be your normal tool though. It’s going to use your own code and test with it down to the millisecond.  We will talk about how that is done in a little while, but first, lets do some learning what we will do before we do it.

Some Technical Details

So, before we start the code, lets start by understanding why we are choosing to use StopWatch in C# and not Something like DateTime to set a start and stop time. We can use DateTime for things that run long time, but may things we are bench testing are milliseconds, not seconds long. DateTime has a threshold of 10 milliseconds. This means if something takes less time than that, you won’t get an accurate measurement. 

StopWatch was actually placed in .NET since 2.0 for this exact scenario. The basics of using StopWatch are quite simple. First You need to instantiate a new instance:

System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();


Then you can start The Stop Watch and start it using the Start and Stop methods of your new object.

// Starts the count
// Some magical function you wish to test
// Stop the count when MyFunction completes

This is the basics of creating a Stop watch and the background to go with it.Next Tutorial we will focus on the Application that will test any application you can think of in a simple library that you can bring around to pretty much anything.


As always, Happy Coding


About Gregg Coleman

I am Senior-level Software Engineer working primarily these days with .NET. I have a good working knowledge of ASP.NET MVC, Web Forms, WCF web services and Windows Services. I spend much of my time in the Web Services (SOAP and REST) world in my current job designing and implementing various SOA architectures. I have been in the software engineering industry for about 6 years now and will not now nor ever consider myself an "expert" in programming because there is always so much to learn. My favorite thing about designing software is there are always new emerging technologies and something to learn every day! My current job has me spending much of my job on the bleeding edge of technologies and changing gears all the time, so I'm never bored and always challenged. On my spare time I enjoy weight training, reading and venturing to new places near by. Of course programing and learning new technologies are another hobby of mine.
This entry was posted in ASP.NET, C#, Performance, Performance Testing, Programming, WPF, XML and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s