🚀 Why is NumPy faster than Python lists?

Irfan CodesIrfan Codes
2 min read

⚡ 1️⃣ Contiguous memory (locality of reference)

  • Python list: Each element is a separate Python object, stored in different memory locations. The list holds pointers to those objects.

  • NumPy array: The data is stored in a single, contiguous block of memory — like a C array.

👉 Why this matters?

  • Modern CPUs use cache to speed up access.

  • If data is contiguous, when one piece is loaded, nearby pieces come into cache too → faster access

  • This is called locality of reference.


⚡ 2️⃣ Fixed data types

  • Python lists can hold mixed types ([1, 'a', 3.14]) → extra overhead to store type info and manage them.

  • NumPy arrays store all elements of the same type → no extra metadata needed → less memory, faster operations.


⚡ 3️⃣ Vectorized operations (SIMD)

  • NumPy uses C code under the hood + CPU vector instructions (SIMD: Single Instruction, Multiple Data) to perform operations on entire arrays at once.

  • Example:

pythonCopyEdita + b  # No loop in Python! NumPy does it in compiled C, very fast.

⚡ 4️⃣ No Python loops (no interpreter overhead)

  • Python loops are slow because of dynamic type checking and interpreter overhead.

  • NumPy avoids loops in Python; operations happen in optimized native code.


🧪 Example: Speed comparison

pythonCopyEditimport numpy as np
import time

# Python list addition
lst1 = list(range(1000000))
lst2 = list(range(1000000))
start = time.time()
lst3 = [x + y for x, y in zip(lst1, lst2)]
print("List time:", time.time() - start)

# NumPy array addition
arr1 = np.array(lst1)
arr2 = np.array(lst2)
start = time.time()
arr3 = arr1 + arr2
print("NumPy time:", time.time() - start)

👉 You’ll see NumPy is 10x to 100x faster!


✅ Summary

FeaturePython ListNumPy Array
Memory layoutScattered (pointers to objects)Contiguous (single memory block)
TypeDynamic (mixed types allowed)Homogeneous (one type)
SpeedSlow for large dataVery fast (vectorized C code)
MemoryMore overheadCompact

Final key point:
👉 NumPy is fast not because Python suddenly became fast — it's because NumPy offloads the heavy lifting to optimized C code that interacts with memory and CPU efficiently.

1
Subscribe to my newsletter

Read articles from Irfan Codes directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Irfan Codes
Irfan Codes