Back
+15 XP
Concept

CPU-Bound vs I/O-Bound Bottlenecks

CPU-bound tasks wait on raw processor computation (matrix math, hashing). I/O-bound tasks spend 99% of time waiting on external systems (disk reads, HTTP APIs) ⏳.

python
import time

# I/O Bound: mostly waiting on external network response 🌐
def fetch_api():
    time.sleep(0.01)  # Simulates remote HTTP delay
    return {"status": "ok"}

# CPU Bound: heavy local processor computation 💻
def crunch_numbers(n):
    return sum(i * i for i in range(n))

# 41. CPU-Bound vs I/O-Bound Bottlenecks CPU-bound tasks wait on raw processor computation (matrix math, hashing). I/O-bound tasks spend 99% of time waiting on external systems (disk reads, HTTP APIs) ⏳. ```python import time # I/O Bound: mostly waiting on external network response 🌐 def fetch_api(): time.sleep(0.01) # Simulates remote HTTP delay return {"status": "ok"} # CPU Bound: heavy local processor computation 💻 def crunch_numbers(n): return sum(i * i for i in range(n)) print("I/O-Bound: Wait for network/disk 🌐") print("CPU-Bound: Wait for processor clock cycles 💻") ``` Knowing the bottleneck dictates your architecture: Use multithreading or asyncio for I/O waits, and multiprocessing for heavy CPU math! ⏳ --- ### Question Which of the following operations is primarily an I/O-bound bottleneck? 🧠 - [ ] a. Waiting for an HTTP REST response from an external microservice 🌐 - [ ] b. Calculating the 100,000th prime number with pure loops - [ ] c. Compressing a raw 8K video frame matrix in RAM - [ ] d. Generating SHA-256 hashes in a cryptographic loop --- ### Challenge Write `classify_task(task_type)` returning `'Network latency wait'` if `task_type == 'io'` else `'Processor heavy crunching'`. Print `classify_task('io')` and `classify_task('cpu')`. 🎯 <details> <summary>Solution</summary> ```python def classify_task(task_type): return "Network latency wait" if task_type == "io" else "Processor heavy crunching" print(classify_task("io")) print(classify_task("cpu")) ``` </details>

Checkpoint

Which of the following operations is primarily an I/O-bound bottleneck? 🧠

Practice

Write `classify_task(task_type)` returning `'Network latency wait'` if `task_type == 'io'` else `'Processor heavy crunching'`. Print `classify_task('io')` and `classify_task('cpu')`. 🎯

Terminal
01
Tap code blocks below to assemble
Available Blocks