Vectorization expresses work over an array or column instead of writing a Python loop for each value. In NumPy, array operations and broadcasting normally use vectorized implementations; they can be orders of magnitude faster than element-by-element Python code. The useful question is not just whether code looks array-oriented, but how it executes. section 4.2.2 section 4.2.4
Choose the operation that matches both the computation and the data shape:
- Use NumPy array operations for elementwise work. Broadcasting applies an operation across compatible shapes without excessive copying; transposing can make a row-oriented operation fit the available broadcasting pattern through a fast view. section 4.2.2
- Do not assume np.vectorize makes a scalar Python function fast. It applies the function element by element and is essentially a loop: an image-brightening example takes milliseconds with np.vectorize but microseconds with multiplication. The scalar function prevents overflow, however, so replacing it requires preserving that behavior. section 4.2.4
- In pandas, prefer column operations to row iteration when the calculation permits it. For a trip-data example, dividing the tip column by the total column expresses a per-trip fraction without traversing rows in Python. section 7.2.2 section 7.6
Check the result before comparing speed
A row-loop baseline accumulates all tips and totals, then divides their sums. The vectorized pandas expression divides each trip’s tip by its total and takes the mean. Those are different calculations: first decide which statistic is wanted, then benchmark equivalent implementations. The example excludes zero totals before division and uses sampled data frames for performance testing. section 7.2.2
def get_tip_mean_vector(df):
frac_tip = df["tip_amount"] / df["total_amount"]
return frac_tip.mean()
A compact pandas column expression for the mean of per-trip tip fractions. section 7.2.2
Vectorized thinking also guides parallel implementations. A CUDA C Mandelbrot kernel accepts an array of positions and computes one value per position, treating the matrix as one-dimensional for thread indexing. In the reported benchmark, its roughly 3.1 ms GPU time is 20 times faster than the compared Numba version; preparing the positions and converting between NumPy and CuPy involve separate work and a transfer. section 9.3.5

















