How do you figure out whether it’s worth parallelizing a particular code block based on its code size? Is the following calculation correct?
Assume:
- Thread pool consisting of one thread per CPU.
- CPU-bound code block with execution time of X milliseconds.
Y = min(number of CPUs, number of concurrent requests)
Therefore:
- Cost: code complexity, potential bugs
- Benefit:
(X * Y)milliseconds
My conclusion is that it isn’t worth parallelizing for small values of X or Y, where “small” depends on how responsive your requests must be.
One thing that will help you figure that out is Amdahl’s Law
Figure out what you want to achieve in speed up, and how much parallelism you can actually achieve, then see if its worth it.