np.savez() for multiple arrays in NumPy - Time & Space Complexity
We want to understand how the time to save multiple arrays with np.savez() changes as the number and size of arrays grow.
How does saving more or bigger arrays affect the time it takes?
Analyze the time complexity of the following code snippet.
import numpy as np
arr1 = np.arange(1000)
arr2 = np.arange(2000)
arr3 = np.arange(3000)
np.savez('arrays.npz', a=arr1, b=arr2, c=arr3)
This code saves three numpy arrays of different sizes into one uncompressed file.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Writing each element of every array to disk.
- How many times: Once for each element in all arrays combined.
As the total number of elements across all arrays grows, the time to save grows roughly the same way.
| Input Size (total elements) | Approx. Operations |
|---|---|
| 10 | About 10 write operations |
| 100 | About 100 write operations |
| 1000 | About 1000 write operations |
Pattern observation: The time grows roughly in direct proportion to the total number of elements saved.
Time Complexity: O(n)
This means the time to save grows linearly with the total number of elements in all arrays combined.
[X] Wrong: "Saving multiple arrays with np.savez() takes constant time regardless of array size."
[OK] Correct: The function must write every element to disk, so more data means more time.
Understanding how saving data scales helps you reason about performance in real projects where data size varies.
What if we compressed the arrays before saving? How would the time complexity change?