Performance tips and vectorization in SciPy - Time & Space Complexity
When using scipy, how we write code affects how fast it runs.
We want to see how using vectorization changes the work done as data grows.
Analyze the time complexity of the following code snippet.
import numpy as np
n = 1000 # Define n before using it
# Create two large arrays
a = np.arange(n)
b = np.arange(n)
# Vectorized addition
c = a + b
This code adds two arrays element-wise using vectorized operations.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Adding each element of array
ato corresponding element ofb. - How many times: Once for each element, so
ntimes.
As the size of the arrays grows, the number of additions grows the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of elements.
Time Complexity: O(n)
This means the time to add arrays grows in a straight line as the arrays get bigger.
[X] Wrong: "Vectorized code runs instantly no matter the size."
[OK] Correct: Vectorization speeds things up but still does work for each element, so time grows with size.
Understanding how vectorization affects time helps you write faster code and explain your choices clearly.
"What if we replaced vectorized addition with a Python loop adding elements one by one? How would the time complexity change?"