Numerical Python·Lesson 23 of 67·intermediate
Vectorisation vs. Python loops
Why the same arithmetic runs a hundred times faster as an array expression, which functions are genuinely vectorised, and how to write a numerically stable softmax.
In this lesson · 18 min
- →Explain where a Python loop's time actually goes
- →Replace loops with ufuncs and reductions
- →Reduce along the right axis, and keep the result stable
- →Recognise that np.vectorize is not a speed-up
- →Write a numerically stable softmax
The same arithmetic written as an array expression instead of a loop typically runs one to two orders of magnitude faster. Understanding why tells you which loops are worth rewriting.
Measure it first
The loop is not slow because multiplication is slow. It is slow because each iteration boxes a
float into a Python object, looks up __mul__, allocates a result object and appends it. The
array expression does the multiplication in a compiled loop over raw memory, once.
Universal functions
A ufunc applies elementwise to a whole array. Every one of these has a math equivalent that
works on one number — and the array version does N of them in one call:
Note the difference between np.maximum (elementwise, two arrays) and np.max (a reduction over
one array). Getting them confused is a classic bug that runs silently.
Reductions and the axis argument
Replacing a loop
Most loops over numbers are one of three shapes: elementwise arithmetic, a reduction, or a dot product.
When you truly must build up a result, preallocate rather than appending — the array already knows how big it is:
np.vectorize is not vectorisation
The name is misleading. It is a convenience wrapper that still calls your Python function once per element:
Softmax, and why stability matters
Softmax turns a vector of scores into probabilities. The textbook formula overflows on real logits:
np.exp(1000) is larger than float64 can hold, so it becomes inf, and inf / inf is nan.
Subtracting the maximum first changes nothing mathematically — the same constant cancels top and
bottom — but keeps every exponent at zero or below:
When a loop is the right answer
Vectorising is not free: it costs readability, and it often costs memory, because intermediate arrays are materialised. A loop is fine when the data is small, when each step depends on the last, or when the body is genuinely complicated:
Try it yourself
Replace the loop with a single array expression that produces the same scores: each document's dot product with the query, divided by the document's length.
Write softmax(logits) so it works on a 1-D array and does not overflow on large values. Every result must sum to 1.
Check yourself
Why is `values ** 2` on a 300,000-element array so much faster than the equivalent list comprehension?
Check yourself
Why does a softmax subtract the maximum logit before exponentiating?
Recap
- A Python loop’s time goes on object overhead, not arithmetic. Array expressions remove it.
- Ufuncs apply elementwise; reductions collapse an axis.
np.maximumandnp.maxare different functions. - Read
axisas “the axis that disappears”. - Preallocate with
np.emptyrather than appending, and use+=orout=to avoid allocating intermediates. np.vectorizeis a convenience, not a speed-up.- Subtract the maximum before
np.exp— the difference between a probability distribution and a vector ofnan. - Keep the loop when steps depend on each other, when the data is small, or when the vectorised version would be unreadable or allocate too much.