Speed testing in .NET

System.Diagnostics.Stopwatch is a replacement for what most people probably do to identify the time spent on executing a method. The process usually goes something like: create a DateTime.Now value at the start and then subtract the ending DateTime.Now to get the TimeDuration value that elapsed.

As an alternative, the Stopwatch class was built using low-level API calls, with less overhead than other .NET methods. If the hardware and Windows version of the computer support a high-resolution performance counter, it will use this counter instead of the standard PC clock.

Here is a simple example:

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

watch.Start();

//do your stuff
//…

watch.Stop();

MessageBox.Show(“Time spent: ” + watch.Elapsed.ToString());

Advertisements

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

%d bloggers like this: