Garbage collection and array references in NumPy - Time & Space Complexity
When working with arrays in numpy, understanding how references and garbage collection affect performance is important.
We want to see how the program's work changes as array sizes grow and references are created or removed.
Analyze the time complexity of the following code snippet.
import numpy as np
a = np.arange(1000000)
b = a
c = a.copy()
d = None
This code creates a large array, assigns references, makes a copy, and removes a reference.
- Primary operation: Copying the array with
a.copy()which duplicates all elements. - How many times: The copy operation touches each element once, so it runs once over all elements.
Copying the array means touching every element, so the work grows as the array gets bigger.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 operations |
| 100 | 100 operations |
| 1000 | 1000 operations |
Pattern observation: The work grows directly with the number of elements; doubling elements doubles the work.
Time Complexity: O(n)
This means the time to copy the array grows linearly with the number of elements.
[X] Wrong: "Assigning b = a copies the array and takes time proportional to its size."
[OK] Correct: Assigning b = a just creates a new reference to the same array without copying data, so it is very fast and does not depend on array size.
Understanding how references and copying affect performance helps you write efficient code and explain memory use clearly in interviews.
"What if we used b = a.view() instead of a.copy()? How would the time complexity change?"