Why vectorized operations matter in NumPy - Performance Analysis
We want to see why using vectorized operations in numpy is faster than doing things step-by-step.
How does the time to finish change when we use vectorized code versus loops?
Analyze the time complexity of the following code snippet.
import numpy as np
arr = np.arange(1_000_000)
# Vectorized operation
result = arr * 2
This code multiplies every number in a large array by 2 using numpy's vectorized operation.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Multiplying each element in the array by 2.
- How many times: Once for each element in the array (1,000,000 times).
As the array size grows, the number of multiplications grows at the same rate.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 multiplications |
| 100 | 100 multiplications |
| 1,000,000 | 1,000,000 multiplications |
Pattern observation: The work grows directly with the number of elements.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the number of elements.
[X] Wrong: "Vectorized operations do all the work instantly, so time does not grow with input size."
[OK] Correct: Even vectorized code must touch each element, so time still grows with the number of elements, just faster than loops.
Knowing why vectorized operations matter shows you understand how to write fast, clean code that handles big data well.
"What if we replaced the vectorized operation with a Python for-loop multiplying each element? How would the time complexity change?"