Home > .NET, Stopwatch, c#, datetime, duration logging > Logging duration with System.Diagnostics.StopWatch instead of using DateTime.Now

Logging duration with System.Diagnostics.StopWatch instead of using DateTime.Now

January 9th, 2009 .NET, Stopwatch, c#, datetime, duration logging

Still using two DateTime.Now calls to determine the duration of a certain call execution, like beneath?

DateTime start = DateTime.Now;
DoSomeCall();
double secondsToLog = (DateTime.Now - start).TotalSeconds;

This is not at all necessary: using the StopWatch class. The StopWatch class is more efficient than any other solution provided or created in .NET since it uses low-level API calls and supports a high-resolution performance counter (when there is hardware and software support).

Stopwatch watch = new Stopwatch();
watch.Start();
DoSomeCall();
watch.Stop();
double secondsToLog = watch.Elapsed.TotalSeconds;
If you like this blog post, you can easily share it:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • DotNetKicks
  • Technorati
  • Twitter
  1. SergeyNikolaev
    July 30th, 2009 at 01:33 | #1

    i found you by link from the Directory Listing Script from Ash.. Nice to read your blog ^.^

  2. Anonymous
    March 7th, 2010 at 03:54 | #2

    Thanks for writing, I very much liked it.

  1. No trackbacks yet.