Files
2026-07-13 13:17:40 +08:00

30 lines
1.4 KiB
ReStructuredText
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
Anti-pattern: Over-parallelizing with too fine-grained tasks harms speedup
==========================================================================
**TLDR:** Avoid over-parallelizing. Parallelizing tasks has higher overhead than using normal functions.
Parallelizing or distributing tasks usually comes with higher overhead than an ordinary function call. Therefore, if you parallelize a function that executes very quickly, the overhead could take longer than the actual function call!
To handle this problem, we should be careful about parallelizing too much. If you have a function or task thats too small, you can use a technique called **batching** to make your tasks do more meaningful work in a single call.
Code example
------------
**Anti-pattern:**
.. literalinclude:: ../doc_code/anti_pattern_too_fine_grained_tasks.py
:language: python
:start-after: __anti_pattern_start__
:end-before: __anti_pattern_end__
**Better approach:** Use batching.
.. literalinclude:: ../doc_code/anti_pattern_too_fine_grained_tasks.py
:language: python
:start-after: __batching_start__
:end-before: __batching_end__
As we can see from the example above, over-parallelizing has higher overhead and the program runs slower than the serial version.
Through batching with a proper batch size, we are able to amortize the overhead and achieve the expected speedup.