Skip to content

Commit 308132d

Browse files
committed
Update notes
1 parent 379bc7c commit 308132d

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

images/latency_4.png

12.6 KB
Loading

notes/performance-in-multithreading.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,30 @@ We can first break the task into multiple independent tasks
1616
![Latency](../images/latency_2.png)
1717

1818
Then we can schedule these sub-tasks to run in parallel to each other in different threads,
19-
and theoretically we want to achieve the latency of `T/N` (N = number of sub-tasks).
19+
and theoretically we want to achieve the latency of `T/N` (`N` = number of sub-tasks).
2020

21-
![Latency](../images/latency_3.png)
21+
![Latency](../images/latency_3.png)
22+
23+
Therefore, we want to do a theoretical reduction in latency by `N` = Performance improvement by a factor of `N`.
24+
25+
### Few important questions
26+
27+
- `N = ?` | How many subtasks/threads to break the original task into?
28+
- Does breaking original task and aggregating results come for free?
29+
- Can we always break any task in multiple subtasks?
30+
31+
#### N = ?
32+
33+
![Latency](../images/latency_4.png)
34+
35+
On a general purpose computer : `N` should be as close as possible to the number of CPU cores (`N = number of cores`)
36+
37+
**Why?**
38+
39+
Theoretically, if nothing else of significance is running on the same computer, then the OS will do the best it can to
40+
schedule every task in a different core utilizing its hardware the best it can and give us the optimum performance.
41+
42+
> Adding just a single additional thread will be **counter-productive** and in fact will reduce the performance and increase the latency.
43+
>
44+
> That additional thread will keep pushing the other threads from their cores back and forth resulting in
45+
> *context-switches*, *bad cache performance* and *extra memory consumption*.

0 commit comments

Comments
 (0)