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.