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)
It could be any number of things. A couple that come to mind
- Overhead from administrating the different threads.
- Other processes are using resources in the system at the same time.
Amdahl's law may explain this
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.
- 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.
- You are running it in an operating system. There are other processes running. These will use up some of the CPU time.
- Maybe you are not anymore CPU-limited but IO-limited (memory bandwidth, disk bandwidth, ...).
- 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.
Because there are other factors to consider. Like memory and I/O bandwidth / contention, thread context switching overhead etc.
精彩评论