Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using .NET Assist's Quick Stopwatch

DZone's Guide to

Using .NET Assist's Quick Stopwatch

· Performance Zone
Free Resource

Download our Introduction to API Performance Testing and learn why testing your API is just as important as testing your website, and how to start today.

.Net Assist - Quick Stopwatch

Often time you need a quick way to time a function or block of code, in early version of .NET you had to write some cumbersome code that relied on the DateTime object. With the introduction of the Stopwatch (.NET 2.0) things got better but still, it took lots of code to insert a simple timing logic:


Insert Declare, initialize and start statements before the code block to time.
Stop and display results after the code block.

Old way:

      string message = "Calculate 800K square roots";
      int roots = 800000;
 
      System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
      sw.Start();
      CalcRoots(roots);
      sw.Stop();
      System.Diagnostics.Trace.WriteLine(string.Format("{0}: {1} (seconds)", 
                                       message, sw.Elapsed.TotalSeconds)); 

Better way:

 using (QuickStopwatch qsw = new QuickStopwatch(message))
        CalcRoots(roots);

I write that kind of quick and dirty timing/benchmarking code all the time. Seeing this QuickStopwatch approach was one of those "pwop" moments. It just seems so darn logical in hindsight, doesn't it?

Find scaling and performance issues before your customers do with our Introduction to High-Capacity Load Testing guide.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}