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()
import time
def slow_function():
total = 0
for i in range(1000000):
total += i
start = time.time()
slow_function()
end = time.time()
print(round(end - start, 2))
What is the expected output type?import time
start = time.time()
for i in range(1000000):
pass
print(end - start)
What is the error and how to fix it?Pass the test and this pattern moves unknown