Skip to content

Commit 4d96d1e

Browse files
committed
Update notes
1 parent 308132d commit 4d96d1e

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

notes/performance-in-multithreading.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ There are two main criteria for performance in multithreaded applications:
55
1. **Latency** - The time to completion of a task. Measured in _time_ units.
66
2. **Throughput** - The amount of tasks completed in a given period. Measured in _tasks/time_ unit.
77

8+
89
## Latency
910

1011
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
2223

2324
Therefore, we want to do a theoretical reduction in latency by `N` = Performance improvement by a factor of `N`.
2425

26+
2527
### Few important questions
2628

2729
- `N = ?` | How many subtasks/threads to break the original task into?
2830
- Does breaking original task and aggregating results come for free?
2931
- Can we always break any task in multiple subtasks?
3032

33+
3134
#### N = ?
3235

3336
![Latency](../images/latency_4.png)
@@ -42,4 +45,22 @@ schedule every task in a different core utilizing its hardware the best it can a
4245
> Adding just a single additional thread will be **counter-productive** and in fact will reduce the performance and increase the latency.
4346
>
4447
> 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!
63+
64+
**References:**
65+
- https://www.hp.com/us-en/shop/tech-takes/what-is-hyperthreading
66+
- https://www.intel.com/content/www/us/en/gaming/resources/hyper-threading.html

0 commit comments

Comments
 (0)