np.save() and np.load() for binary in NumPy - Time & Space Complexity
We want to understand how the time to save and load data with numpy changes as the data size grows.
How does the time cost grow when saving or loading bigger arrays?
Analyze the time complexity of the following code snippet.
import numpy as np
n = 1000 # example size
arr = np.arange(n) # create an array of size n
np.save('data.npy', arr) # save array to binary file
loaded_arr = np.load('data.npy') # load array from binary file
This code creates an array of size n, saves it to a binary file, then loads it back into memory.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading and writing each element of the array to disk.
- How many times: Once for each element in the array, so n times.
As the array size grows, the time to save or load grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 operations (reading/writing 10 elements) |
| 100 | 100 operations |
| 1000 | 1000 operations |
Pattern observation: Doubling the input size roughly doubles the time needed.
Time Complexity: O(n)
This means the time to save or load grows linearly with the number of elements.
[X] Wrong: "Saving or loading is instant regardless of data size."
[OK] Correct: The process must handle each element, so bigger arrays take more time.
Knowing how data saving and loading scales helps you understand performance in real projects.
"What if we compressed the file while saving? How would the time complexity change?"