Why NumPy is Fast
Speed matters. Think of it like using a sports car instead of a bicycle for a race. NumPy's performance comes from smart design choices.
Vectorization
Vectorization means operating on entire arrays at once instead of element-by-element.
import numpy as np
import time
size = 1000000
arr = np.arange(size)
start = time.time()
result = arr * 2
print(f"Vectorized: {time.time() - start:.4f}s")
start = time.time()
result = np.array([x * 2 for x in arr])
print(f"Loop: {time.time() - start:.4f}s")
Vectorized operations can be 10-100x faster than Python loops!
C Under the Hood
NumPy is written in C, which is much faster than Python for numerical operations.
import numpy as np
import sys
py_list = list(range(1000))
np_arr = np.arange(1000)
print(f"Python list size: {sys.getsizeof(py_list)} bytes")
print(f"NumPy array size: {np_arr.nbytes} bytes")
Here is the thing - NumPy uses less memory and does operations faster because of C implementation.
Contiguous Memory
NumPy stores data in contiguous memory blocks, which improves cache performance.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(f"C contiguous: {arr.flags['C_CONTIGUOUS']}")
print(f"Memory layout: {arr.strides}")
One thing that confused me at first was why Python lists are slow. They store pointers to objects, not the objects themselves.
Try it Yourself →Key Takeaways
- Vectorization operates on entire arrays at once
- NumPy is implemented in C for speed
- Contiguous memory improves cache performance
- NumPy uses less memory than Python lists