.NET 中 System.nanoTime() 的等效项是什么?

发布于 2024-08-07 04:32:45 字数 146 浏览 4 评论 0原文

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

看了这里,但没有多大帮助。

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

Looked here, but it isn't much helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

手心的温暖 2024-08-14 04:32:45

我认为 Stopwatch 类就是你的意思寻找。

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

2024-08-14 04:32:45

如果您希望在 GNU/Linux 和 Windows(至少 7 个)下的不同进程、不同语言(Java、C、C#)之间比较时间戳:

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

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;
}
时光是把杀猪刀 2024-08-14 04:32:45

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

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

滴情不沾 2024-08-14 04:32:45

我能找到的最接近的是 DateTime.ToFileTime() 方法。您可以在 DateTime 的实例上调用此方法,如下所示:

long starttime = DateTime.Now.ToFileTime()

该方法返回 Windows 文件时间:

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

您至少可以将时间间隔缩短至 100 ns。

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

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

生生不灭 2024-08-14 04:32:45

DateTime.Now.Ticks

我试图找到这个问题的答案来运行一些性能测试。

DateTime startTime = DateTime.Now;
generatorEntity.PopulateValueList();
TimeSpan elapsedTime = DateTime.Now - startTime;
Console.WriteLine("Completed! time(ticks) - " + elapsedTime.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);
吃不饱 2024-08-14 04:32:45

我认为如果您以纳秒为单位进行计时,您将达到操作系统的硬限制。这是一篇关于该主题的好文章:

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文