Broadcasting performance implications in NumPy - Time & Space Complexity
When using numpy, broadcasting lets us do operations on arrays of different shapes easily.
We want to know how this affects the time it takes to run these operations as arrays get bigger.
Analyze the time complexity of the following code snippet.
import numpy as np
large_array = np.ones((1000, 1000))
small_array = np.arange(1000)
result = large_array + small_array[np.newaxis, :]
This code adds a small 1D array to a large 2D array using broadcasting.
- Primary operation: Element-wise addition of arrays after broadcasting.
- How many times: Once for each element in the resulting array (1000 x 1000 = 1,000,000 times).
As the large array size grows, the number of additions grows too, since each element is processed.
| Input Size (n x n) | Approx. Operations |
|---|---|
| 10 x 10 | 100 |
| 100 x 100 | 10,000 |
| 1000 x 1000 | 1,000,000 |
Pattern observation: Operations grow roughly with the total number of elements in the large array.
Time Complexity: O(n^2)
This means the time to complete the operation grows roughly with the square of the array dimension size.
[X] Wrong: "Broadcasting makes the operation as fast as the smaller array size."
[OK] Correct: Even if one array is small, numpy still processes every element in the larger array, so time depends on the big array size.
Understanding how broadcasting affects performance helps you write efficient numpy code and explain your reasoning clearly in interviews.
What if we replaced the small 1D array with a scalar value? How would the time complexity change?