.NET Tip of The Day
Learn one new .NET trick every day
Быстрое пополнение счета телефона      Login or Join

Speed testing in .NET - System.Diagnostics.Stopwatch

System.Diagnostics.Stopwatch is a replacement for what most people probably do to identify the time spent on excecuting 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 my stuff

...

 

watch.Stop();

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



9/20/2007

Comments:

Hey, it would take a long time to put this on every page of your site while testing.
Is there any way to stick in the MasterPage?

Aron 11/7/2007 10:23:48 PM

I'd suggest to create a PageBase class and derive all other pages from this class.

kostya.ly 11/7/2007 11:18:29 PM

why not use tracing if you are talking about asp.net pages. it gives you everything right from size of each n every control to the time taken to load.

krishna 11/28/2007 8:57:39 PM

Name
URL
E-mail
Provide your e-mail address to receive notification about new comments.
Message
HTML tags are not supported.
Please add 4 and 2 and type the answer here:
RSS .NET Tip of The Day
Subscribe to receive one tip from the .NET Tips and Tricks Community per day.
Previous Tips of The Day
The best of the .NET Tips & Tricks Community.
.NET Practitioners .NET Tips & Tricks Community
Every .NET practitioner has a trick up in their sleeve. This is the place to share it with other .NET people.
Submit a Tip
Discovered a new trick? Share it with others.
My Tips
Manage tips you authored.