np.vstack() and np.hstack() in NumPy - Time & Space Complexity
We want to understand how the time needed to join arrays grows as the arrays get bigger.
How does stacking arrays vertically or horizontally affect the work done by numpy?
Analyze the time complexity of the following code snippet.
import numpy as np
arr1 = np.ones((1000, 10))
arr2 = np.zeros((1000, 10))
vstacked = np.vstack((arr1, arr2))
hstacked = np.hstack((arr1, arr2))
This code stacks two arrays: one on top of the other (vertical) and side by side (horizontal).
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Copying all elements from input arrays into a new larger array.
- How many times: Once for each element in the input arrays.
As the size of the input arrays grows, the time to copy all elements grows proportionally.
| Input Size (rows x cols) | Approx. Operations |
|---|---|
| 10 x 10 | ~200 (copying elements twice) |
| 100 x 10 | ~2,000 |
| 1000 x 10 | ~20,000 |
Pattern observation: The operations grow roughly in direct proportion to the total number of elements copied.
Time Complexity: O(n)
This means the time to stack arrays grows linearly with the total number of elements involved.
[X] Wrong: "Stacking arrays is instant or very fast regardless of size because it just links arrays together."
[OK] Correct: Actually, numpy creates a new array and copies all elements, so the time depends on how many elements are copied.
Knowing how array operations scale helps you write efficient data processing code and explain your choices clearly in interviews.
What if we stacked three or more arrays instead of two? How would the time complexity change?