C# .NET 中 System.nanoTime() 的等价物是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1551742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:45:35  来源:igfitidea点击:

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

c#java.netnanotime

提问by Lazlo

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

标题几乎是不言自明的,我因为这种简单而自杀。

Looked here, but it isn't much helpful.

看过here,但没有太大帮助。

采纳答案by Guffa

I think that the Stopwatchclass is what you are looking for.

我认为Stopwatch类是您正在寻找的。

回答by Guffa

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

DateTime.Now将以毫秒为单位为您提供当前时间,但精确到纳秒的时间是相当不切实际的,至少在 Windows 中。

回答by Scott M.

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

我能找到的最接近的是 DateTime.ToFileTime() 方法。您可以像这样在 DateTime 的实例上调用它:

long starttime = DateTime.Now.ToFileTime()

The method returns a Windows File Time:

该方法返回一个 Windows 文件时间:

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).

Windows 文件时间是一个 64 位值,表示自公元 1601 年 1 月 1 日午夜 12:00 起经过的 100 纳秒间隔数(公元 1601 年 1 月 1 日)协调世界时 (UTC)。

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

你至少可以用它把时间缩短到 100 ns 的时间间隔。

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

源代码:http: //msdn.microsoft.com/en-us/library/system.datetime.tofiletime.aspx

回答by overstood

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

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.

虽然 Windows 很乐意返回 100 纳秒的精度,但时钟只能保证每 15.6 毫秒左右更新一次。因此,Windows 将这些更新发生的时间有效地返回到 100 纳秒的精度。为了获得比这更高的准确性,您可能需要准备编写 C 或汇编程序并运行和嵌入式操作系统。

回答by Mike Payne

DateTime.Now.Ticks

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);

回答by Aubin

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

如果您想在 GNU/Linux 和 Windows(至少七种)下,在不同进程、不同语言(Java、C、C#)之间比较时间戳:

Java:

爪哇:

java.lang.System.nanoTime();

C GNU/Linux:

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:

C 窗口:

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#:

C#:

private static long nanoTime() {
   long nano = 10000L * Stopwatch.GetTimestamp();
   nano /= TimeSpan.TicksPerMillisecond;
   nano *= 100L;
   return nano;
}