多核和线程感知.Net 秒表?
众所周知,秒表可能在将在所有内核上运行的多线程异步应用程序中出现问题。
在多处理器计算机上,线程在哪个处理器上运行并不重要。但是,由于 BIOS 或硬件抽象层 (HAL) 中的错误,您可能会在不同的处理器上获得不同的计时结果。要指定线程的处理器关联性,请使用 ProcessThread.ProcessorAffinity 方法。
有什么方法可以让我获得一些可靠的刻度/时间戳之类的值,这些值具有高分辨率/准确性,并且在 CPU 核心之间保持一致?
如果无法使秒表(QueryPerformanceCounter)多核安全,那么获得跨核良好时间戳的下一个最佳方法是什么? DateTime.UtcNow.Ticks 还是Environment.TickCount?还有其他柜台吗?
我确实需要比 DateTime.UtcNow.Ticks 提供的更好的分辨率。 (10-15 毫秒)
As we all know the stopwatch might have issues in a multi-threaded async application that will be running on all cores.
On a multiprocessor computer, it does not matter which processor the thread runs on. However, because of bugs in the BIOS or the Hardware Abstraction Layer (HAL), you can get different timing results on different processors. To specify processor affinity for a thread, use the ProcessThread.ProcessorAffinity method.
Are there any way for me to get some reliable tick/timestamp like values that has high resolution/accuracy as well is consistent across cpu cores?
If not possible to make the stopwatch(QueryPerformanceCounter) multi-core safe, what is the next best way to get a good timestamp across cores? DateTime.UtcNow.Ticks or Environment.TickCount? Any other counters?
I do need better resolution than what DateTime.UtcNow.Ticks can provide. (10-15ms)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我自己寻找了这个特定问题的答案,我见过的最好的答案是 DateTime.UtcNow。似乎没有 Windows 公开的高分辨率和可靠计数器(我已经在 google 上搜索了很长时间,但仍然没有找到任何计数器)。
I have searched for an answer to this specific problem myself and the best that I have seen is DateTime.UtcNow. It would seem that there is no Windows exposed high resolution and reliable counter (I have googled for one for ages and still haven't come across any).
实际上,根据 官方文档,Windows XP 之后,在多处理器系统上使用 StopWatch / QueryPerformanceCounter 应该没问题。如果系统不支持不变TSC,QPC将自动使用不同的定时器策略。
不过,请注意虚拟化系统的评论部分。看来在这种特殊情况下你必须小心。
Actually, according to the official documentation, post-Windows XP, it should be just fine to use StopWatch / QueryPerformanceCounter on multiprocessor systems. If the system does not support invariant TSC, QPC will automatically use a different timer strategy.
Do note the comment section, though, for virtualized systems. It seems that you have to be careful in that specific case.