是System.Diagnostics.Stopwatch类的计时器分辨率稳定吗? [英] Is the timer resolution of the System.Diagnostics.Stopwatch class stable?

查看:295
本文介绍了是System.Diagnostics.Stopwatch类的计时器分辨率稳定吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

净具有使用 System.Diagnostics.Stopwatch 类高分辨率定时支持。据我所知,这个类使用不同的特定分辨率取决于底层硬件,可以通过静态属性获得 Stopwatch.Frequency

.Net has support for high resolution timing using the System.Diagnostics.Stopwatch class. I understand that the specific resolution that this class uses differs depending on the underlying hardware and can be obtained via the static property Stopwatch.Frequency.

该频率似乎与CPU频率和秒表在读取静态初始化/构造此值,并将其存储在一个静态类变量。因此,我现在想,如果这个类会当CPU时钟变化报告不正确的时机?例如在改变CPU时钟根据系统的负载系统。

This frequency appears to be related to CPU frequency and Stopwatch reads this value and stores it in a static class variable within a static initializer/constructor. Hence I'm now wondering if this class will report incorrect timings if the CPU clock changes? e.g. in systems that alter the CPU clock depending system load.

推荐答案

MSDN:

*的频率值取决于底层计时机制的分辨率。如果安装的硬件和操作系统支持高分辨率性能计数器,则该频率值反映该计数器的频率。否则,该频率值是基于系统计时器频率。*

在Stopwatch.Frequency的来源是不是CPU频率。它是一种高频率的硬件计数器的频率。在Windows上,计数器频率通常也是通过调用读取到的 QueryPerformanceCounter函数而在Linux上高分辨率POSIX定时器用于获取高分辨率的时机。

The source of the Stopwatch.Frequency is not the CPU frequency. It is the frequency of a high frequency hardware counter. On windows this counter frequency is typically also read by a call to the QueryPerformanceCounter function while on linux High Resolution POSIX Timers are used to get high resolution timing.

使用时间戳计数器(RDTSC - CPU频率)往往是在这样的背景下讨论但没有真正的意义:

The use of Time Stamp Counter (RDTSC - CPU frequency) is often discussed in this context but has no real relevance:

某些较旧的硬件可能不用于提供定时,因此CPU的频率被用作替换提供任何高频硬件。当使用CPU频率定时它需要被固定,该频率不保持恒定。更现代化的硬件提供了动态CPU频率修改。使用CPU的工频突变量作为衡量,而其动态修改将结束在MESSS。

Some older hardware may not provide any high frequency hardware for timing purposes, thus the frequency of the CPU is used as a replacement. When using the CPU frequency for timing it needs to be secured that the frequency does remain constant. More modern hardware provides dynamic CPU frequency modification. Using the CPU freqency as a measure while it is dynamically modified would end in a messs.

幸运的是,这是不存在的的情况。它可以修改自己的CPU主频系统更加现代化。这些现代化的系统也确实有一个独立的高频计时器硬件高precision事件定时器

Fortunately this is non existing situation. Systems which can modify their CPU frequency are more modern. Such modern systems also do have a separate high frequency timer hardware High Precision Event Timer.

有关于使用CPU频率多核系统的一些传言。实际上出现了一定的硬件问题。但是,这是很久以前的事和固定一段时间。

There are some rumors about using the CPU frequency on multicore systems. In fact there has been a problem with certain hardware. But that's long time ago and fixed for some time.

这篇关于是System.Diagnostics.Stopwatch类的计时器分辨率稳定吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆