为什么我的秒表频率这么低?
Debug.WriteLine("Timer is high-resolution: {0}", Stopwatch.IsHighResolution);
Debug.WriteLine("Timer frequency: {0}", Stopwatch.Frequency);
结果:
Timer is high-resolution: True
Timer frequency: 2597705
这篇文章(2005 年开始!)提到了频率3579545,超过我的100万。 这篇博客文章提到频率为 3,325,040,000,这太疯狂了。
为什么我的频率相对较低?我使用的是 i7 920 机器,那么它不是应该更快吗?
Debug.WriteLine("Timer is high-resolution: {0}", Stopwatch.IsHighResolution);
Debug.WriteLine("Timer frequency: {0}", Stopwatch.Frequency);
Result:
Timer is high-resolution: True
Timer frequency: 2597705
This article (from 2005!) mentions a Frequency of 3579545, a million more than mine. This blog post mentions a Frequency of 3,325,040,000, which is insane.
Why is my Frequency so much comparatively lower? I'm on an i7 920 machine, so shouldn't it be faster?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
3,579,545 是一个神奇的数字。这是以赫兹为单位的频率,然后将其除以 3 并将其输入原始 IBM PC 中的 8053 定时器芯片。这个看起来奇怪的数字并不是偶然选择的,它是颜色突发信号的频率美国和日本使用的 NTSC 电视系统。 IBM 工程师正在寻找一种廉价的晶体来实现振荡器,没有什么比每台电视机中使用的晶体更便宜的了。
一旦 IBM 克隆产品广泛使用,其设计人员选择相同的频率仍然很重要。许多 MS-DOS 软件都依赖于以该速率计时的计时器。直接寻址芯片是一种常见的犯罪行为。
Windows 出现后,情况发生了变化。 Windows 2 的一个版本是第一个虚拟化定时器芯片的版本。换句话说,软件不再允许直接寻址定时器芯片。处理器被配置为在保护模式下运行并拦截使用 I/O 指令的尝试。相反,运行内核代码,从而允许伪造指令的返回值。现在可以让多个程序使用计时器,而不会互相干扰。打破对硬件实际实现方式的依赖是重要的第一步。
Win32 API(Windows NT 3.1 和 Windows 95)通过 API、QueryPerformanceCounter() 和 QueryPerformanceFrequency() 正式化了对计时器的访问。内核级组件(硬件适配层)允许 BIOS 通过该频率。现在,硬件设计人员可以真正放弃对确切频率的依赖。顺便说一句,这花了很长时间,在 2000 年左右,绝大多数机器仍然具有遗留率。
但对削减 PC 设计成本的永无休止的追求终结了这一点。如今,硬件设计人员只需选择芯片组中恰好可用的任何频率即可。 3,325,040,000就是这样一个数字,它很可能是CPU时钟频率。这样的高频在廉价设计中很常见,尤其是那些具有 AMD 核心的设计。你的号码很不寻常,你的机器可能不便宜。而且计时器要准确得多,CPU 时钟具有典型的电子元件容差。
3,579,545 is the magic number. That's the frequency in Hertz before dividing it by 3 and feeding it into the 8053 timer chip in the original IBM PC. The odd looking number wasn't chosen by accident, it is the frequency of the color burst signal in the NTSC TV system used in the US and Japan. The IBM engineers were looking for a cheap crystal to implement the oscillator, nothing was cheaper than the one used in every TV set.
Once IBM clones became widely available, it was still important for their designers to choose the same frequency. A lot of MS-DOS software relied on the timer ticking at that rate. Directly addressing the chip was a common crime.
That changed once Windows came around. A version of Windows 2 was the first one to virtualize the timer chip. In other words, software wasn't allowed to directly address the timer chip anymore. The processor was configured to run in protected mode and intercepted the attempt to use the I/O instruction. Running kernel code instead, allowing the return value of the instruction to be faked. It was now possible to have multiple programs using the timer without them stepping on each other's toes. An important first step to break the dependency on how the hardware is actually implemented.
The Win32 API (Windows NT 3.1 and Windows 95) formalized access to the timer with an API, QueryPerformanceCounter() and QueryPerformanceFrequency(). A kernel level component, the Hardware Adaption Layer, allows the BIOS to pass that frequency. Now it was possible for the hardware designers to really drop the dependency on the exact frequency. That took a long time btw, around 2000 the vast majority of machines still had the legacy rate.
But the never-ending quest to cut costs in PC design put an end to that. Nowadays, the hardware designer just picks any frequency that happens to be readily available in the chipset. 3,325,040,000 would be such a number, it is most probably the CPU clock rate. High frequencies like that are common in cheap designs, especially the ones that have an AMD core. Your number is pretty unusual, some odds that your machine wasn't cheap. And that the timer is a lot more accurate, CPU clocks have typical electronic component tolerances.
频率取决于 HAL(硬件抽象层)。早在奔腾时代,使用 CPU 时钟周期(基于 CPU 时钟频率)是很常见的,因此最终会得到非常高频率的定时器。
对于多处理器和多核机器,尤其是可变速率 CPU(CPU 时钟在低功耗状态下会减慢),使用 CPU 滴答作为计时器变得困难且容易出错,因此 HAL 的编写者似乎选择了使用速度较慢但更可靠的硬件时钟,例如实时时钟。
The frequence depends on the HAL (Hardware abstraction layer). Back in the pentium days, it was common to use the CPU tick (which was based on the CPU clock rate) so you ended up with really high frequency timers.
With multi-processor and multi-core machines, and especially with variable rate CPUs (the CPU clock slows down for low power states) using the CPU tick as the timer becomes difficult and error prone, so the writers of the HAL seem to have chosen to use a slower, but more reliable hardware clock, like the real time clock.
Stopwatch.Frequency 值是每秒,因此频率 2,597,705 意味着每秒超过 250 万次滴答。您到底需要多少精度?
至于频率的变化,这是一个依赖于硬件的事情。一些最常见的硬件差异是核心数量、每个核心的频率、CPU(或核心)的当前电源状态、是否启用操作系统动态调整 cpu 频率等。您的频率不会总是相同的,并且根据您检查时您的CPU处于什么状态,它可能会更低或更高,但通常大约相同(对您来说,可能约为250万。)
The Stopwatch.Frequency value is per second, so your frequency of 2,597,705 means you have more than 2.5 million ticks per second. Exactly how much precision do you need?
As for the variations in frequency, that is a hardware-dependent thing. Some of the most common hardware differences are the number of cores, the frequency of each core, the current power state of your cpu (or cores), whether you have enabled the OS to dynamically adjust the cpu frequency, etc. Your frequency will not always be the same, and depending on what state your cpu is in when you check it, it may be lower or higher, but generally around the same (for you, probably around 2.5 million.)
我认为 2,597,705 = 你的处理器频率。我的数字是 2,737,822。 i7 930
I think 2,597,705 = your processor frequency. Myne is 2,737,822. i7 930