在四核上使用线程可以将代码速度提高 65%?

发布于 2024-08-31 21:47:07 字数 277 浏览 7 评论 0原文

此示例代码在四核处理器上对串行方法与线程方法进行了比较。该代码仅使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

浅忆 2024-09-07 21:47:08

它可以是任意数量的事情。我想到

  1. 了管理不同线程的一些开销。
  2. 其他进程同时使用系统中的资源。

It could be any number of things. A couple that come to mind

  1. Overhead from administrating the different threads.
  2. Other processes are using resources in the system at the same time.
蓦然回首 2024-09-07 21:47:08

最有可能的是,它必须与其他线程竞争某些数据结构或文件,这样您就无法获得 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.

无敌元气妹 2024-09-07 21:47:08

线程有开销,并且并非所有内容都可以始终并行运行。

  • 创建/启动/停止线程的时间。
  • 完成的额外工作(如锁定/递增)
  • 并非每个资源都可以完全并行,例如内存访问或读/写文件。
  • 操作系统和其他应用程序可能时不时地需要一些处理器时间。

Threading has overhead, and not everything can always run in parallel.

  • Time to create/start/stop threads.
  • Extra work done (like locking/incrementing)
  • Not every resource might be perfectly parallelizable , e.g. memory access, or reading/writing files.
  • The OS and other apps might need some of the processor time every now and then.
垂暮老矣 2024-09-07 21:47:08
  1. 您正在操作系统中运行它。还有其他进程正在运行。这些会占用一些 CPU 时间。
  2. 也许你不再受 CPU 限制,而是 IO 限制(内存带宽、磁盘带宽……)。
  3. 线程(和线程切换)、编组等总是会产生一些开销。

总的来说,根据我的并行编程经验,如果您的运行时间减少 65%,那就相当不错了。

  1. You are running it in an operating system. There are other processes running. These will use up some of the CPU time.
  2. Maybe you are not anymore CPU-limited but IO-limited (memory bandwidth, disk bandwidth, ...).
  3. There always will be some overhead for threading (and thread-switching), marshalling, etc.

Overall, from my parallel programming experience, if you get an elapsed time of 65% less, that is pretty good.

泅渡 2024-09-07 21:47:08

因为还有其他因素需要考虑。例如内存和 I/O 带宽/争用、线程上下文切换开销等。

Because there are other factors to consider. Like memory and I/O bandwidth / contention, thread context switching overhead etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文