The Darkness Behind DateTime.Now
page 4 of 7
by Keyvan Nayyeri
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 40709/ 67

Stopwatch

A lesser-used aspect of DateTime calculations in .NET is Stopwatch type that is designed to assist programmers to calculate the runtime performance of a piece of code. Most programmers tend to use DateTime.Now and DateTime.UtcNow before and after a piece of code to find the duration of time that it takes to execute.

Stopwatch relies on a public static method called GetTimeStamp which works in two modes. If it’s not used in high resolution mode, it applies DateTime.UtcNow, otherwise it applies QueryPerformanceCounter from Windows API (listing 3) that I will discuss in the next section.

Listing 3: GetTimeStamp implementation in Stopwatch

public static long GetTimestamp()
{
      if (Stopwatch.IsHighResolution)
      {
            long result = 0L;
            SafeNativeMethods.QueryPerformanceCounter(out result);
            return result;
      }
      return DateTime.UtcNow.Ticks;
}

View Entire Article

User Comments

Title: 你们不能明白   
Name: 巴拿马
Date: 2012-12-08 3:41:36 AM
Comment:
巴拿马






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-11-28 11:54:32 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search