Performance Comparison: NumPy vs Python Loops for Dot Product¶
This notebook compares the performance of NumPy's optimized vectorized operations and a standard Python loop for calculating the dot product of two large vectors.
In [16]:
import numpy as np
import time
import pandas as pd
# Generate random data
size = 1_000_000
a = np.random.rand(size)
b = np.random.rand(size)
# Measure time for NumPy dot product
start_numpy = time.time()
numpy_result = np.dot(a, b)
end_numpy = time.time()
# Measure time for Python loop dot product
start_loop = time.time()
loop_result = sum(a[i] * b[i] for i in range(size))
end_loop = time.time()
# Record times
numpy_time = end_numpy - start_numpy
loop_time = end_loop - start_loop
numpy_time, loop_time
pd.DataFrame({'numpy_time': [numpy_time], 'loop_time': [loop_time]}).head()
print(f"NumPy is {loop_time / numpy_time:.2f} times faster than Python loop.")
NumPy is 545.88 times faster than Python loop.
Results¶
The execution times for the two methods are as follows:
- NumPy implementation: Time recorded is stored in
numpy_time
. - Python loop implementation: Time recorded is stored in
loop_time
.
Run the cell above to observe the difference in execution times.
Observations¶
On my machine, the NumPy implementation is approximately 5.45x times faster than the Python loop implementation. This is because NumPy uses optimized C code to perform the vectorized operations, which is much faster than the standard Python loop.