What is the equivalent to System.nanoTime() in .NET?

前端 未结 7 1429
暖寄归人
暖寄归人 2021-01-07 20:22

The title is pretty much self-explanatory, I\'m killing myself over this simplicity.

Looked here, but it isn\'t much helpful.

相关标签:
7条回答
  • 2021-01-07 20:35

    the closest thing that i could find is the DateTime.ToFileTime() method. you can call this on an instance of a DateTime like so:

    long starttime = DateTime.Now.ToFileTime()
    

    The method returns a Windows File Time:

    A Windows file time is a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed since 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC).

    you could at least time down to 100 ns intervals with it.

    src: http://msdn.microsoft.com/en-us/library/system.datetime.tofiletime.aspx

    0 讨论(0)
  • 2021-01-07 20:35

    DateTime.Now.Ticks

    I was trying to find the answer to this to run some performance testing.

    DateTime startTime = DateTime.Now;
    generatorEntity.PopulateValueList();
    TimeSpan elapsedTime = DateTime.Now - startTime;
    Console.WriteLine("Completed! time(ticks) - " + elapsedTime.Ticks);
    
    0 讨论(0)
  • 2021-01-07 20:38

    I think you're going to hit the hard limits of the OS if you're timing in nanoseconds. Here's a good article on the topic:

    http://www.lochan.org/2005/keith-cl/useful/win32time.html

    While Windows will happily return 100 nanosecond accuracy, the clock is only guaranteed to update once every 15.6 milliseconds or so. So effectively Windows returns the time at which those updates occurred to 100 nanosecond accuracy. For more accuracy than this you probably need to be prepared to write C or assembler and run and embedded OS.

    0 讨论(0)
  • 2021-01-07 20:43

    I think that the Stopwatch class is what you are looking for.

    0 讨论(0)
  • 2021-01-07 20:47

    http://msdn.microsoft.com/de-de/library/system.datetime.ticks.aspx

    somelike: DateTime.Ticks

    0 讨论(0)
  • 2021-01-07 20:48

    DateTime.Now will give you the current time in milliseconds, but time that is accurate to nanoseconds is fairly impractical, at least in Windows.

    0 讨论(0)
提交回复
热议问题