views:

244

answers:

6

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)

+4  A: 

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.
Kevin
@Kevin, I am sure there are no other processes are using processors heavily
Ahmed Said
There is still a strong possibility that the thread administration is causing the drop in expected performance.
Kevin
+2  A: 

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.

Lasse V. Karlsen
+4  A: 

Amdahl's law may explain this

Osama ALASSIRY
+1  A: 

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

HTH,
Kent

Kent Boogaart
+1  A: 
  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.

Daniel Rose
+2  A: 

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.
nos