Timing code (poor-mans instrumentation)

Let’s say you want to time how long something takes to run. Well,  before .NET 2.0, maybe you capture the start time and end time – and then try to fumble through some way to calculate the elapsed time. Then in .NET 2.0 – maybe it got one step better because you could calculate a “TimeSpan” from the two times. Well, there is one step better – it’s System.Diagnostics.Stopwatch. For example:
 
Stopwatch currentStopwatch = new Stopwatch();
currentStopwatch.Start();
 
// execute some code
 
currentStopwatch.Stop();
MessageBox.Show(currentStopwatch.Elapsed.ToString());
 
That outputs a very high resolution output, for example:
 
00:00:00.0000117
 
In a pinch, if you don’t have a better way to instrument or profile code – this makes it a little easier.
Posted in General, Uncategorized

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Archives
Categories

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 5 other followers

%d bloggers like this: