Problem Statement
Why are NumPy operations often faster than pure Python loops?
Explanation
NumPy stores data in contiguous memory blocks and performs vectorized math in optimized C loops. This avoids Python’s per-element overhead and speeds up arithmetic and broadcasting.
The speedup is highest for large numeric arrays. For tiny arrays, Python overhead can dominate, and gains may be small.
Code Solution
SolutionRead Only
import numpy as np x=np.arange(1_000_000) y=x*2 # vectorized C loop
Practice Sets
This question appears in the following practice sets:
