Controlling copy behavior in NumPy - Time & Space Complexity
We want to understand how the time cost changes when we control copying of arrays in numpy.
How does making a copy or view affect the work done as data size grows?
Analyze the time complexity of the following code snippet.
import numpy as np
arr = np.arange(1000000)
view_arr = arr[100:200] # creates a view, no data copied
copy_arr = arr[100:200].copy() # creates a copy, data copied
result_view = view_arr * 2
result_copy = copy_arr * 2
This code creates a large array, then makes a view and a copy of a slice, and multiplies each by 2.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Multiplying each element in the slice by 2.
- How many times: Once for each element in the slice (100 elements).
Execution time grows with the number of elements processed.
| Input Size (slice length) | Approx. Operations |
|---|---|
| 10 | About 10 multiplications |
| 100 | About 100 multiplications |
| 1000 | About 1000 multiplications |
Pattern observation: The time to multiply grows linearly with the slice size.
Time Complexity: O(n)
This means the time to multiply elements grows directly with the number of elements.
[X] Wrong: "Creating a view copies all the data, so it takes a lot of time."
[OK] Correct: A view just references the original data without copying, so it is fast and uses little extra time.
Knowing how copying and views affect time helps you write efficient code and explain your choices clearly.
"What if we replaced the slice multiplication with an operation on the entire array? How would the time complexity change?"