在四核上使用线程可以将代码速度提高 65%?
此示例代码在四核处理器上对串行方法与线程方法进行了比较。该代码仅使用 GetPixel() 读取 4 个图像中的所有像素。 我发现速度提升了 65% 左右,为什么我有 4 个核心,而且它们都被充分利用,但它不等于 75%?
PS:
你能检查一下代码吗,因为我没有做任何I/O,并且机器上没有其他进程正在运行(正常的Windows进程)
This sample code compares serial method with threaded method, on Quad core processor. The code just uses GetPixel() to read all pixels from 4 images.
I found that the speed up is around 65%, why it does not equal 75% as I have 4 cores and all of them are fully utilized?
P.S:
Can you check the code as I do not do any I/O, and no other processes are working on the machines (normal windows processes)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它可以是任意数量的事情。我想到
It could be any number of things. A couple that come to mind
阿姆达尔定律可以解释这一点
Amdahl's law may explain this
最有可能的是,它必须与其他线程竞争某些数据结构或文件,这样您就无法获得 100% 并行执行。
例如,如果您要在四核上以 4 路并行方式运行从网站下载网页类型的操作,并且服务器一次只允许从同一 IP 地址进行 1 个并发下载,那么您将无法根本没有任何加速。
此外,还有一些与旋转线程和维护线程相关的开销,因此当您开始并行时,您不会获得完整的核心使用率,尽管在这种情况下这很可能不是一个大因素。
Most likely it has to compete with the other threads on some data structure, or file, so that you don't get 100% parallel execution.
For instance, if you were to run a download-webpage-from-website type of operation in 4-way parallel on a quad-core, and the server only allows 1 concurrent download from the same IP address at a time, you won't get any speedup at all.
Also, there's some overhead related to spinning up threads and maintaining them, so you won't get a full core's usage when you start doing parallel, though it is most likely not a big factor in this case.
线程有开销,并且并非所有内容都可以始终并行运行。
Threading has overhead, and not everything can always run in parallel.
总的来说,根据我的并行编程经验,如果您的运行时间减少 65%,那就相当不错了。
Overall, from my parallel programming experience, if you get an elapsed time of 65% less, that is pretty good.
因为还有其他因素需要考虑。例如内存和 I/O 带宽/争用、线程上下文切换开销等。
Because there are other factors to consider. Like memory and I/O bandwidth / contention, thread context switching overhead etc.