Jan 26, 2026
# Chapter 16 — Performance and Profiling (When Code Feels Slow)
This chapter is “measurement first”.
## 16.1 The Rule
Don’t guess. Measure.
## 16.2 Time a Small Block
```python
import time
start = time.perf_counter()
total = 0
for i in range(1_000_000):
total += i
end = time.perf_counter()
print("seconds:", end - start)
```
## 16.3 `timeit` (More Reliable Micro Benchmark)
```python
import timeit
code = "sum(range(1000))"
print(timeit.timeit(code, number=10000))
```
---