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

7

23

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

Looked here, but it isn't much helpful.

Schapira answered 11/10, 2009 at 20:53 Comment(0)
M
29

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

Mesarch answered 11/10, 2009 at 20:56 Comment(3)
+1, For timing (vs. getting the absolute time), Stopwatch is it.Andante
Stopwatch is definitely the best way to time things in the BCL, it's just not accurate to nanoseconds.Lueck
@overstood: Yes, the accuracy is not down to nanoseconds, just like the nanoTime method. :)Mesarch
H
16

If you want a timestamp to be compared between different processes, different languages (Java, C, C#), under GNU/Linux and Windows (Seven at least):

Java:

java.lang.System.nanoTime();

C GNU/Linux:

static int64_t hpms_nano() {
   struct timespec t;
   clock_gettime( CLOCK_MONOTONIC, &t );
   int64_t nano = t.tv_sec;
   nano *= 1000;
   nano *= 1000;
   nano *= 1000;
   nano += t.tv_nsec;
   return nano;
}

C Windows:

static int64_t hpms_nano() {
   static LARGE_INTEGER ticksPerSecond;
   if( ticksPerSecond.QuadPart == 0 ) {
      QueryPerformanceFrequency( &ticksPerSecond );
   }
   LARGE_INTEGER ticks;
   QueryPerformanceCounter( &ticks );
   uint64_t nano = ( 1000*1000*10UL * ticks.QuadPart ) / ticksPerSecond.QuadPart;
   nano *= 100UL;
   return nano;
}

C#:

private static long nanoTime() {
   long nano = 10000L * Stopwatch.GetTimestamp();
   nano /= TimeSpan.TicksPerMillisecond;
   nano *= 100L;
   return nano;
}
Headstall answered 23/5, 2017 at 13:40 Comment(0)
S
1

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

Seaway answered 11/10, 2009 at 20:57 Comment(1)
I believe the accuracy of DateTime.Now is ~15 or 20 milliseconds. It has more precision than accuracy.Scrim
W
1

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

Wheedle answered 11/10, 2009 at 21:6 Comment(1)
This only gives you 100 ns accuracy for the time of the last windows timer update, which happens every 15 to 16 msLueck
W
1

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);
Whimper answered 22/11, 2014 at 22:38 Comment(0)
L
0

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.

Lueck answered 11/10, 2009 at 22:15 Comment(0)
S
0

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

somelike: DateTime.Ticks

Stanwinn answered 18/5, 2011 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.