NumPy vs. Python Lists โ€” Speed Test for Data Performance

https://youtu.be/YYMaMTW-2hw

If you’ve ever wondered whether to use plain Python lists or NumPy arrays for numerical computations, you’re not alone. In this tutorial, we put them head-to-head in a series of speed tests. Youโ€™ll see exactly when NumPy becomes the clear winner, especially with large datasets.


๐ŸŽ“ What Are We Testing?

We’ll compare Python lists and NumPy arrays in three operations:

  • Summing elements
  • Element-wise multiplication
  • Memory usage

Each test runs on a dataset with 10 million values to simulate real-world data loads like scientific computing or image processing.


โš™๏ธ Setup and Data Generation

Import libraries and generate data

import time
import numpy as np

# Data size
N = 10_000_000

# Python list and NumPy array
py_list = list(range(N))
np_array = np.arange(N)

We create equivalent datasets in both formats so the comparison is fair.


โฑ๏ธ Speed Test 1 โ€” Summing Elements

Using Python list:

start = time.time()
sum_py = sum(py_list)
end = time.time()
print("Python list sum:", end - start, "seconds")

Using NumPy:

start = time.time()
sum_np = np.sum(np_array)
end = time.time()
print("NumPy sum:", end - start, "seconds")

NumPy significantly outperforms native Python thanks to low-level optimizations.


๐Ÿ”ข Speed Test 2 โ€” Element-wise Multiplication

Using Python list:

start = time.time()
py_mult = [x * 2 for x in py_list]
end = time.time()
print("Python list multiplication:", end - start, "seconds")

Using NumPy:

start = time.time()
np_mult = np_array * 2
end = time.time()
print("NumPy array multiplication:", end - start, "seconds")

No loops, no hassle โ€” NumPy performs these operations in compiled C, making it lightning fast.


๐Ÿ“Š Memory Efficiency Comparison

Letโ€™s compare how much memory each structure uses:

print("Python list size:", len(py_list) * py_list[0].__sizeof__(), "bytes")
print("NumPy array size:", np_array.nbytes, "bytes")

NumPy arrays are not only faster but also more compact in memory, which is crucial for data-heavy tasks.


๐Ÿ“„ Wrap-Up

NumPy isnโ€™t always the right tool โ€” but when dealing with large numerical arrays, itโ€™s a game-changer. You get:

  • Faster computation
  • Cleaner, vectorized code
  • Lower memory usage

If you’re working with math-heavy applications, NumPy is a must-learn.


๐Ÿ“Œ Summary

  • Compared Python lists and NumPy arrays
  • Benchmarked summing and multiplying operations
  • Measured memory usage
  • Observed significant performance gains with NumPy

๐Ÿ”— Resources

  • NumPy Documentation
  • NumPy Tutorials (Real Python)
  • Python Time Complexity Wiki

Stick around for our next deep dive into vectorized operations and broadcasting in NumPy!

Leave a Reply

Your email address will not be published. Required fields are marked *