Introduction
Imagine your computer program is running slower than expected, but you don't know why. Finding the exact part that causes the slowdown is crucial to fix it and make the program faster.
Imagine a busy highway where one lane is closed, causing traffic jams. Even if other lanes are clear, cars slow down at the closed lane. Fixing that lane opens the flow for all cars.
┌─────────────────────────────┐ │ Program Flow │ ├─────────────┬───────────────┤ │ Fast Parts │ Bottleneck │ │ (Quick) │ (Slow) │ ├─────────────┴───────────────┤ │ Overall speed limited by slowest part │ └─────────────────────────────┘
import time def fast_part(): time.sleep(0.1) # Simulates a quick task def slow_part(): time.sleep(0.5) # Simulates a slow task def main(): start = time.perf_counter() fast_part() slow_part() end = time.perf_counter() print(f"Total time: {end - start:.2f} seconds") if __name__ == "__main__": main()