Why NumPy performance matters - Performance Analysis
We want to understand how fast NumPy runs when working with data.
How does the time it takes change when the data gets bigger?
Analyze the time complexity of the following code snippet.
import numpy as np
n = 10
arr = np.arange(n)
result = np.sum(arr)
This code creates an array of numbers from 0 to n-1 and then adds all those numbers together.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding each number in the array.
- How many times: Once for each number in the array (n times).
As the array gets bigger, the time to add all numbers grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: Doubling the input doubles the work needed.
Time Complexity: O(n)
This means the time to finish grows directly with the size of the data.
[X] Wrong: "Adding numbers in NumPy is instant no matter the size."
[OK] Correct: Even though NumPy is fast, it still needs to look at each number once to add it, so bigger arrays take more time.
Knowing how NumPy handles data size helps you explain why some tasks take longer and shows you understand efficient data work.
"What if we used np.sum on a 2D array instead of 1D? How would the time complexity change?"