Vectorisation vs Loops
Python · NumPy Library
Theory
Vectorisation means writing an operation for the whole array, such as total = price * qty, instead of looping over elements in Python. The loop version runs the Python interpreter once per element, checking types every time. The vectorised version hands the whole array to NumPy's compiled code, which loops in C over values packed in memory, so it is often 10 to 100 times faster on large arrays.
Most loops can be replaced: element-wise maths with operators and ufuncs, if-else with np.where, filters with boolean masks, and running totals with np.cumsum. Vectorised code is usually shorter and closer to the maths too. The trade-off is memory, because each step creates a temporary array; for very large data, work in chunks. A loop is still fine for small lists or logic that depends on the previous result.
Why we use it
Data pipelines touch millions of values. Replacing Python loops with array operations turns minutes into seconds and also makes the code shorter and easier to read.
When to use it
Reach for vectorised operations whenever you loop over numbers in an array or DataFrame column. Keep a plain loop for small data, complex step-by-step logic, or calls to functions that cannot take arrays.
How it works
- Python loop over array elements
- Is each step independent of the previous result? Yes: Rewrite with operators, ufuncs, np.where or masks. No: Keep the loop or use np.cumsum style helpers.
- Check the vectorised result equals the loop result
- Time both versions on realistic data
- Faster, shorter code
Syntax
# loop
out = [p * q for p, q in zip(price, qty)]
# vectorised
out = price * qty
np.where(cond, a, b) np.cumsum(a)Example
price = np.arange(1, 1_000_001)
total = price * 1.18 # one line, no loopKey points
- Vectorised code runs the loop in C, not in Python
- Use np.where for if-else and masks for filtering
- Speed gains grow with array size; tiny arrays barely differ
- Each step creates a temporary array, so watch memory
- Timings vary by machine; compare on your own data