Profiling with line_profiler in Data Analysis Python - Time & Space Complexity
Profiling helps us see which parts of our code take the most time.
We want to find out how the time spent grows as input gets bigger.
Analyze the time complexity of the following code snippet.
from line_profiler import LineProfiler
def compute_squares(numbers):
result = []
for n in numbers:
result.append(n * n)
return result
lp = LineProfiler()
lp.add_function(compute_squares)
lp.enable_by_count()
numbers = list(range(1000))
lp_wrapper = compute_squares
lp_wrapper(numbers)
lp.disable()
lp.print_stats()
This code measures how long each line in compute_squares takes when squaring numbers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the list
numbersto square each number. - How many times: Once for each number in the input list.
As the list gets bigger, the time to square all numbers grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 squaring steps |
| 100 | 100 squaring steps |
| 1000 | 1000 squaring steps |
Pattern observation: Doubling the input roughly doubles the work done.
Time Complexity: O(n)
This means the time grows directly with the number of items to process.
[X] Wrong: "Profiling will always show constant time for small inputs."
[OK] Correct: Even small inputs take time proportional to their size; profiling reveals this clearly.
Knowing how to profile helps you find slow parts in your code and explain their time cost clearly.
"What if we changed the loop to process numbers in pairs? How would the time complexity change?"