什么是测量经过时间在现代PC的最准确的方法是什么? [英] What's the most accurate way of measuring elapsed time in a modern PC?

查看:194
本文介绍了什么是测量经过时间在现代PC的最准确的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用IRQ0,这是系统定时器,但是这是基于14.31818MHz时钟,对不对?有什么提供更大precision?

I know I can use IRQ0, which is the system timer, but this is based on a 14.31818MHz clock, right? Is there anything offering greater precision?

感谢。

编辑:有谁知道Windows函数QueryPerformanceCounter的使​​用

Does anyone know what the Windows function QueryPerformanceCounter uses?

推荐答案

precision和准确性的意思是不同的事情。 地球的周长是40000.000000000公里是precise,但并不准确。这是一个比较复杂的钟表:

"Precision" and "accuracy" mean different things. "The Earth's circumference is 40000.000000000 km" is precise, but not accurate. It's a bit more complicated with clocks:


  • 解析:蜱,蜱或期限之间的时间。 (你也许可以称之为precision,但我认为决议具有更明显的意义。)

  • 倾斜:名义和实际时钟频率(ISH)
  • 之间的相对差异
  • 漂移:(由于老化,温度等),歪斜的变化率

  • 抖动:蜱定时随机变化

  • 延迟:需要多长时间获得时间戳

  • Resolution: time between ticks, or period of ticks. (You could probably call it "precision", but I think "resolution" has a more obvious meaning.)
  • Skew: relative difference between nominal and actual clock frequency (ish)
  • Drift: rate of change of skew (due to aging, temperature, ...).
  • Jitter: Random variation in tick timing.
  • Latency: How long it takes to get a timestamp.

尽管系统定时器( PIT 根据维基百科)在1.something运行兆赫,你通常能获得IRQ0某处100和1000 Hz之间的。 显然你也可以从口两次0X40获取当前的计数器值阅读,但我不知道什么样的延迟这有(然后你会得到计数的数量,直到下一个中​​断,所以你需要做一些数学)。它也没有更多的现代滴答​​的内核工作。

Even though the "system timer" (PIT according to Wikipedia) runs at 1.something MHz, you generally get IRQ0 somewhere between 100 and 1000 Hz. Apparently you can also read from from port 0x40 twice to get the current counter value, but I'm not sure what kind of latency this has (and then you get number of counts until the next interrupt, so you need to do some math). It also doesn't work on more modern "tickless" kernels.

有一些其他的高频计时器:

There are a few other high-frequency timers:


  • 本地APIC,它是基于总线频率和功率的2分频。我无法找到如何虽然读它的任何文件(presumably它是一个I / O端口?)。

  • ACPI电源管理计时器(acpi_pm在Linux中,我认为,和/ UsePMTimer Windows启动标志),这是根据的这个。 IIRC,看完有点贵。

  • HPET,这至少是10兆赫根据相同链路(但它可以更高)。它也应该有比ACPI PM计时器更低的延迟。

  • TSC(含警告)。几乎可以肯定的最低延迟,也可能是最高频率为好。 (但很显然它可​​以超过1每一个滴答走了,所以计数每秒不一定是相同的分辨率。)

  • Local APIC, which is based on the bus frequency and a power-of-2 divider. I can't find any documentation on how to read it though (presumably it's an I/O port?).
  • ACPI power management timer (acpi_pm in Linux; I think, and the /UsePMTimer Windows boot flag), which is about 3.58 MHz according to this. IIRC, reading it is a bit expensive.
  • HPET, which is at least 10 MHz according to the same link (but it can be higher). It's also supposed to have lower latency than the ACPI PM timer.
  • TSC (with caveats). Almost certainly the lowest latency, and probably the highest frequency as well. (But apparently it can go up by more than 1 every "tick", so the counts-per-second isn't necessarily the same as the resolution.)

达尔文(即OS Ⅹ)似乎假定TSC频率不改变,并调整从那里将TSC​​没有运行(显然C4和更高)睡眠状态醒来时添加到它的基准值。有每个CPU不同的基值,因为TSC不必在CPU同步。你必须把在努力得到一个合理的时间戳的合理费用。

Darwin (i.e. OS X) appears to assume that the TSC frequency does not change, and adjusts the base value added to it when waking up from a sleep state where the TSC is not running (apparently C4 and greater). There's a different base value per CPU, because the TSC need not be synchronized across CPUs. You have to put in a reasonable amount of effort to get a sensible timestamp.

IIRC,Linux的只是挑选一个时钟源(TSC,如果是理智的,然后HPET,然后下午ACPI,我认为)。

IIRC, Linux just picks a single clock source (TSC, if it's sane, and then HPET, and then ACPI PM, I think).

IIRC,QueryPerformanceCounter的()使用任何Windows认为是最好的。这一定程度上取决于Windows版本太(XP理应不支持HPET用于中断,所以presumably如果不是确实为时间戳其一)。你可以调用QueryPerformanceFrequency的()进行推测(我得到1995030000,这可能意味着它的TSC)。

IIRC, QueryPerformanceCounter() uses whatever Windows thinks is best. It depends somewhat on Windows version too (XP supposedly doesn't support HPET for interrupts, so presumably it doesn't for timestamps either). You can call QueryPerformanceFrequency() to make a guess (I get 1995030000, which probably means it's the TSC).

这篇关于什么是测量经过时间在现代PC的最准确的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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