5

Possible Duplicate:
C# DateTime.Now precision

There are a few questions on SO regarding high-resolution timing in .NET where it is stated (as in the MSDN documentation) that the DateTime.Ticks has a resulution of 100 nano-seconds.

But this seem not to be the case at all in real-life. If I run the code below, I would expect it to generate lines, where the Tick value varies for each line, where a lot of lines are with the the same Milisecond value. But it doesn't - the ticks value stays the same until next shift in milisecond value, adding nothing to the resolution of the time-stamp.

private static List<string> GetTimeLine(long iterations)
{
    List<string> liste = new List<string>();
    for (long i = 0; i <= iterations; i++)
    {
        liste.Add(DateTime.Now.Millisecond.ToString() + " - " + DateTime.Now.Ticks.ToString());
    }

    return liste;
}

static void Main(string[] args)
{
    Console.WriteLine("Generating timeline");
    guids = GetTimeLine(10000);
    File.WriteAllLines(@"C:\Test\GUIDS.TXT", guids);
    Console.WriteLine("File written - Press ENTER");
    Console.ReadLine();
}

Example of result output:

...
867 - 634940160118679615

867 - 634940160118679615

867 - 634940160118679615

867 - 634940160118679615

867 - 634940160118679615

868 - 634940160118689616

868 - 634940160118689616

868 - 634940160118689616

...

So what is the method for getting more than miliseconds resolution in .NET?

Community
  • 1
  • 1
Jesper R. Hansen
  • 405
  • 1
  • 4
  • 16
  • 6
    [Precision and accuracy of DateTime](http://blogs.msdn.com/b/ericlippert/archive/2010/04/08/precision-and-accuracy-of-datetime.aspx) _"If the question you want to ask is about how long some operation took, and you want a high-precision, high-accuracy answer, then use the StopWatch class. It really does have nanosecond precision and accuracy that is close to its precision."_ – Tim Schmelter Jan 17 '13 at 10:12
  • 2
    This isn't a problem with the resolution of `DateTime.Ticks` but with the precision of `DateTime.Now`, and for an answer to that, see [C# DateTime.Now precision](http://stackoverflow.com/questions/2143140/c-sharp-datetime-now-precision) (which I suppose led to that blog post). – Rawling Jan 17 '13 at 10:12
  • 1
    Please, consider using Stopwatch: http://msdn.microsoft.com/es-es/library/system.diagnostics.stopwatch.aspx – Didac Perez Parera Jan 17 '13 at 10:15
  • All of your questions are answered in this very good article: http://blogs.msdn.com/b/ericlippert/archive/2010/04/08/precision-and-accuracy-of-datetime.aspx – bitbonk Jan 17 '13 at 10:16
  • Ah great you added the link back (It seemed missing, when I wrote that comment). – bitbonk Jan 17 '13 at 10:20
  • Thank you for the links, which confirmed what I am experiencing. I think it would be of value if this was included in the description on MSDN for the DateTime.Ticks Property. – Jesper R. Hansen Jan 17 '13 at 10:25

1 Answers1

5

The most accurate way of measuring elapsed time in C# is to use the Stopwatch class.

This uses the microprocessor's performance counter, implemented with calls to QueryPerformanceCounter.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • No, the most accurate way of measuring elapsed time in C# is multimedia timers. As stated in the the earlier answer by lazyberezovsky. – bitbonk Jan 17 '13 at 10:24
  • 6
    No, it's the most accurate way of scheduling timer events, which is NOT THE SAME as simply measuring elapsed time! We are talking about measuring elapsed time, not scheduling events! I was very careful to explicitly say "measuring elapsed time" in my answer. See http://support.microsoft.com/kb/172338?wa=wsignin1.0 which says "When timing code to identify performance bottlenecks, you want to use the highest resolution timer the system has to offer. This article describes how to use the QueryPerformanceCounter function to time application code." – Matthew Watson Jan 17 '13 at 10:31