Array creation (array, arange, linspace) in Data Analysis Python - Time & Space Complexity
When we create arrays using functions like array, arange, or linspace, it is important to understand how the time to create these arrays grows as the size increases.
We want to know how the work done changes when we ask for bigger arrays.
Analyze the time complexity of the following code snippet.
import numpy as np
size = 1000
arr1 = np.array(list(range(size)))
arr2 = np.arange(size)
arr3 = np.linspace(0, 10, size)
This code creates three arrays of the same size using three different NumPy functions.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Creating each element of the array one by one.
- How many times: Exactly
sizetimes for each array creation.
As the requested array size grows, the time to create the array grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 operations |
| 100 | 100 operations |
| 1000 | 1000 operations |
Pattern observation: Doubling the size roughly doubles the work needed to create the array.
Time Complexity: O(n)
This means the time to create the array grows linearly with the number of elements you want.
[X] Wrong: "Creating an array with linspace is faster because it just calculates start and end points."
[OK] Correct: Even though linspace calculates evenly spaced points, it still needs to generate each element, so the time grows with the number of points.
Understanding how array creation scales helps you reason about data preparation steps in real projects, showing you can think about efficiency beyond just writing code.
"What if we create an array by repeating a single value n times? How would the time complexity change?"