.Net 4.0 代码将在 Core 2 Duo 上运行,但不能在 i5 上运行。需要建议
我写了一些非常复杂的多线程代码。它使用旋转、产量和互锁库。
该代码在我的 Core 2 Duo 2 GHz 机器上完美运行。在我的 i5 2.4 GHz 双核四线程机器上,它很慢(比 Core 2 慢),或者完全无法运行(只是 CPU 利用率为 0%)。
两台机器都有 Win 7 Home Premium 64 位和 .Net 4.5 框架。我的开发机器是 Core 2 Duo。我在 Visual Studio 2011 Dev Preview 中进行开发。该代码是在优化的情况下编译的。
我无法在此处粘贴多行代码。任何人都可以开始告诉我为什么会发生这种情况/在我的代码中寻找什么。我对 .Net 大会的这个结果感到有点震惊。
编辑
我测试了 MS 的 BlockingCollection 的速度,这也只是 i5 速度的一小部分,但 4 个“核心”都是 100% = 某种类型的竞争条件。然而,在这里我只是简单地调用 TryAdd() / Take() 方法,而没有旋转或任何其他类型的线程控制。
I have written some very complex multithreaded code. It uses spinning, yields, and the Interlocked library.
The code runs flawlessly on my Core 2 Duo 2 GHz machine. On my i5 2.4 GHz dual core quad thread machine, it is slow (as in slower than the Core 2), or totally fails to run (simply sits at 0% CPU).
Both machines have Win 7 Home Premium 64 bit and the .Net 4.5 framework. My dev machine is the Core 2 Duo. I developed in Visual Studio 2011 Dev Preview. The code is compiled with optimizations on.
I cannot paste the multiple lines of code here. Can anyone even begin to tell me why this could be happening / what to look for in my code. I am a little shocked at this outcome from a .Net assembly.
EDIT
I tested the speed of MS's BlockingCollection, and that too was a fraction of the speed on the i5, but the 4 "cores" were all 100% = some type of race condition. However, here I am simply calling the TryAdd() / Take() methods with no spinning or any other type of thread control on my part.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您极有可能遇到竞争状况。症状都在那里 - 您有复杂的线程代码,这些代码要么不运行,要么不按照您期望的方式运行,并且行为因机器而异。它在双核机器上运行的事实并不能证明代码是无竞争的...更多核心意味着出现微妙竞争的可能性更高。
简而言之,我们需要代码来提供帮助。
您提到您正在使用“旋转类” - 您是指 SpinLock 和 SpinWait 吗?
It is extremely likely that you are experiencing a race condition. The symptoms are all there- you have complex threading code that either doesn't run or doesn't run the way you expect it should, and the behavior varies across machines. The fact that it runs on the dual core box doesn't prove that the code is race free... More cores means a higher likelihood that subtle races will show up.
In short, we need the code to help.
You mention that you are using "spinning class"- do you mean SpinLock and SpinWait?
在没有任何代码的情况下,我只能推测:
众所周知,Core i5 具有 Intel 超线程技术,该技术将硬件线程数量增加到 4 个,而只有 2 个核心,因此实际上有四个线程竞争相同的 CPU 功能单元。这意味着计算密集型应用程序或需要大量内存带宽的应用程序在采用 HT 技术的 CPU 上使用时可能会出现性能下降。尝试将应用程序生成的线程数固定为 2(这样您实际上就不会使用 HT 功能),并查看它是否像在原始双核上一样运行。
另请查看这篇文章,了解 HT 的功能和缺点:http://software.intel.com/en-us/articles/performance-insights-to-intel-hyper-threading-technology/
这个(有些)相关的问题:多线程 Java 没有加速
Without any code, I can only speculate:
The Core i5 is known to have Intel hyperthreading technology which brings the number of hardware threads to 4 with only 2 cores, so you effectively have four threads competing for the same CPU functional units. This means that compute intensive applications or applications that need a lot of memory bandwidth may suffer a performance degradation when used on CPUs with HT technology. Try to fix the number of threads spawned by your application to 2 (such that you are effectively not using the HT feature) and see if it runs like on the original dual core.
Also check this article for features and weaknesses of HT: http://software.intel.com/en-us/articles/performance-insights-to-intel-hyper-threading-technology/
And this (somewhat) related SO question: Multithreaded Java does not speed up