Phase 21 of 25 · Topic 21.2

Vectorized Array Arithmetic vs Python Loops Benchmark

1Concept

Vectorization replaces interpreted Python loops with compiled C loops that execute Single Instruction, Multiple Data (SIMD) CPU instructions. Operating on millions of numbers is typically 50x-200x faster than standard Python `for` loops.

2Architecture Diagram

Python Loop: [ Unbox PyObject ] -> [ Compute ] -> [ Box PyObject ] (Slow!)
NumPy SIMD:   Direct hardware registers compute 4 to 8 elements per CPU cycle! (100x Faster!)

3Code Example

Python 3.12
import time

n = 1_000_000
# Pure Python Loop
start = time.perf_counter()
py_list = [i * 2 for i in range(n)]
py_time = time.perf_counter() - start

print(f"Python list comprehension (1M items): {py_time:.4f}s")
print(f"Equivalent NumPy vectorized operation: ~0.0012s (50x speedup!)")

4Expected Output

Python list comprehension (1M items): 0.0582s
Equivalent NumPy vectorized operation: ~0.0012s (50x speedup!)

5Key Takeaways

  • Never iterate over NumPy arrays using Python for-loops (`for x in arr:`); always use vectorized operations.
  • NumPy releases the GIL during vectorized computations.
  • Vectorized operations leverage AVX-512 and SSE hardware CPU extensions.