什么是计时器刻度,Stopwatch.ElapsedTicks 使用的单位

发布于 2024-11-03 10:47:36 字数 199 浏览 5 评论 0原文

我曾经认为

  • Stopwatch.ElapsedTicks 等于
  • Stopwatch.Elapsed.Ticks

但事实并非如此。后者是 100 纳秒的周期,而前者则使用一个名为“计时器滴答”的神秘单位。我想知道它们是什么,为什么它们与通常的测量单位不同?

I used to think that

  • Stopwatch.ElapsedTicks was equal to
  • Stopwatch.Elapsed.Ticks.

But it isn't. While the latter is a number of 100 nanoseconds periods, the former use a mysterious unit called timer ticks. I wonder what those are, and why are they different from the usual unit of measurement?

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

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

发布评论

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

评论(3

狼性发作 2024-11-10 10:47:36

来自文档

秒表通过计算底层计时器机制中的计时器滴答数来测量经过的时间。如果安装的硬件和操作系统支持高分辨率性能计数器,则 Stopwatch 类将使用该计数器来测量经过的时间。否则,Stopwatch 类使用系统计时器来测量经过的时间。使用频率IsHighResolution 字段来确定秒表计时实现的精度和分辨率。

是的,“滴答声”被重载意味着两个不同的东西,这是一个痛苦:(

我认为这基本上是因为 Stopwatch 提供了性能计数器的一个非常“原始”的视图,它可以有不同的实现“勾号”是什么意思。

From the docs:

The Stopwatch measures elapsed time by counting timer ticks in the underlying timer mechanism. If the installed hardware and operating system support a high-resolution performance counter, then the Stopwatch class uses that counter to measure elapsed time. Otherwise, the Stopwatch class uses the system timer to measure elapsed time. Use the Frequency and IsHighResolution fields to determine the precision and resolution of the Stopwatch timing implementation.

Yes, it's a pain that "ticks" is overloaded to mean two different things :(

I think it's basically because Stopwatch is giving a pretty "raw" view of a performance counter, which can have different implementations for what it means by a "tick".

豆芽 2024-11-10 10:47:36

Stopwatch.ElapsedTicks 正在测量秒表的“滴答声”,即 1 秒的时间长度/Stopwatch.Frequency

在内部,这基于 Windows 高性能计数器支持(如果支持)由您的系统决定,这几乎总是正确的)。本机调用是 QueryPerformanceFrequency,其长度根据您的硬件而有所不同支持。

The Stopwatch.ElapsedTicks is measuring a "tick" in terms of the stopwatch, which is a length of time of 1 second/Stopwatch.Frequency.

Internally, this is based on the Windows High Performance Counter Support (if supported by your system, which is almost always true). The native call is QueryPerformanceFrequency, which will vary in length depending on your hardware's support.

っ〆星空下的拥抱 2024-11-10 10:47:36

也许它有助于查看控制台输出...

LinkedList 500 插入/删除操作 ElapsedTicks : 45871
LinkedList 500 次插入/删除操作 Elapsed.ticks : 141266
LinkedList 500 次插入/删除操作 毫秒:14.1266
列出 500 个插入/删除操作 ElapsedTicks: 1235121
列出 500 个插入/删除操作 Elapsed.ticks : 3803744
列出 500 个插入/删除操作 毫秒:380.3744

(从中可以确认 Elapsed.Ticks 的测量时间为 100 纳秒)

Maybe it helps to see console output ...

LinkedList 500 insert/remove operations ElapsedTicks : 45871
LinkedList 500 insert/remove operations Elapsed.ticks : 141266
LinkedList 500 insert/remove operations milisec: 14.1266
List 500 insert/remove operations ElapsedTicks: 1235121
List 500 insert/remove operations Elapsed.ticks : 3803744
List 500 insert/remove operations milisec: 380.3744

(From which it is confirmed that Elapsed.Ticks are measured in 100 nanoseconds)

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