Why vectorize in Python
The same sum, three ways, and the reason numpy exists.
import timeit
import numpy as np
values = np.arange(100_000, dtype=np.float64)
def with_loop():
total = 0.0
for value in values:
total += value
return total
def with_numpy():
return values.sum()
loop = timeit.timeit(with_loop, number=3) / 3
fast = timeit.timeit(with_numpy, number=3) / 3
print(f"loop {loop * 1000:.1f} ms")
print(f"numpy {fast * 1000:.3f} ms")
print(f"{loop / fast:.0f}x")
How it works
- A Python loop pays interpreter cost per element.
- A ufunc does the loop in compiled code.
timeiton a small array can mislead: measure the real size.
Keywords and builtins used here
asdefforprintreturnwith_loopwith_numpy
The run, in numbers
- Lines
- 23
- Characters to type
- 398
- Tokens
- 122
- Three-star pace
- 110 tpm
At the three-star pace of 110 tokens a minute, this run takes about 67 seconds.
Step 4 of 4 in Maths & memory, step 19 of 22 in NumPy.