You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: notes/performance-in-multithreading.md
+22-1Lines changed: 22 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -5,6 +5,7 @@ There are two main criteria for performance in multithreaded applications:
5
5
1.**Latency** - The time to completion of a task. Measured in _time_ units.
6
6
2.**Throughput** - The amount of tasks completed in a given period. Measured in _tasks/time_ unit.
7
7
8
+
8
9
## Latency
9
10
10
11
Suppose we have a single task which can be completed by a single thread sequentially within time T
@@ -22,12 +23,14 @@ and theoretically we want to achieve the latency of `T/N` (`N` = number of sub-t
22
23
23
24
Therefore, we want to do a theoretical reduction in latency by `N` = Performance improvement by a factor of `N`.
24
25
26
+
25
27
### Few important questions
26
28
27
29
-`N = ?` | How many subtasks/threads to break the original task into?
28
30
- Does breaking original task and aggregating results come for free?
29
31
- Can we always break any task in multiple subtasks?
30
32
33
+
31
34
#### N = ?
32
35
33
36

@@ -42,4 +45,22 @@ schedule every task in a different core utilizing its hardware the best it can a
42
45
> Adding just a single additional thread will be **counter-productive** and in fact will reduce the performance and increase the latency.
43
46
>
44
47
> 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*.
48
+
> *context-switches*, *bad cache performance* and *extra memory consumption*.
49
+
50
+
**Important points**
51
+
52
+
-`# threads = # cores` is optimal only if all threads are runnable and can run without interruption (no IO / blocking calls / sleep etc.)
53
+
- The assumption is that nothing else is running that consumes a lot of CPU.
54
+
55
+
56
+
#### Hyper-threading
57
+
58
+
Most computers today use **Hyper-threading**. It means a single physical core can run more than one thread at a time.
59
+
60
+
Hyper-threading is a process by which a CPU divides up its **physical cores** into **virtual cores** that are treated as if they are actually physical cores by the OS.
61
+
62
+
We cannot run all the threads 100% in parallel, but we can get close to that!
0 commit comments