Vectorized operations vs loops in Data Analysis Python - Performance Comparison
We want to see how fast different ways of processing data grow when the data gets bigger.
Specifically, we compare doing tasks step-by-step in loops versus doing them all at once with vectorized operations.
Analyze the time complexity of the following code snippet.
import numpy as np
# Using a loop to square each element
arr = np.arange(1_000_000)
squared_loop = []
for x in arr:
squared_loop.append(x ** 2)
# Using vectorized operation to square all elements
squared_vectorized = arr ** 2
This code squares each number in a large list once using a loop, then again using a vectorized operation.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Squaring each element in the array.
- How many times: Once per element, so as many times as the array length.
- Loop version: Explicit loop runs n times.
- Vectorized version: Operation applied once but internally handles all n elements.
As the number of elements grows, the loop does more repeated steps, while vectorized code handles all at once more efficiently.
| Input Size (n) | Approx. Operations (Loop) | Approx. Operations (Vectorized) |
|---|---|---|
| 10 | 10 steps | 1 step (handles all) |
| 100 | 100 steps | 1 step (handles all) |
| 1000 | 1000 steps | 1 step (handles all) |
Pattern observation: Loop steps grow directly with input size, vectorized stays roughly constant in code but does work internally.
Time Complexity: O(n)
This means the time grows linearly with the number of elements, but vectorized operations run much faster in practice.
[X] Wrong: "Vectorized operations have constant time complexity and are always faster regardless of data size."
[OK] Correct: Vectorized operations still process every element, so time grows with data size, but they use optimized code that runs faster than explicit loops.
Understanding how loops and vectorized operations scale helps you write faster data code and explain your choices clearly in interviews.
"What if we used nested loops to square elements in a 2D array? How would the time complexity change?"