什么是计时器刻度,Stopwatch.ElapsedTicks 使用的单位
我曾经认为
Stopwatch.ElapsedTicks
等于Stopwatch.Elapsed.Ticks
。
但事实并非如此。后者是 100 纳秒的周期,而前者则使用一个名为“计时器滴答”的神秘单位。我想知道它们是什么,为什么它们与通常的测量单位不同?
I used to think that
Stopwatch.ElapsedTicks
was equal toStopwatch.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
来自文档:
是的,“滴答声”被重载意味着两个不同的东西,这是一个痛苦:(
我认为这基本上是因为
Stopwatch
提供了性能计数器的一个非常“原始”的视图,它可以有不同的实现“勾号”是什么意思。From the docs:
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".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.
也许它有助于查看控制台输出...
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)