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?"
Practice
line_profiler in Python data analysis?Solution
Step 1: Understand profiling purpose
line_profileris designed to measure execution time per line in functions.Step 2: Compare options to profiling goal
Options A, B, and D relate to data cleaning, visualization, and type conversion, not profiling.Final Answer:
To measure the time each line of a function takes to execute -> Option AQuick Check:
Profiling = measuring line execution time [OK]
- Confusing profiling with data visualization
- Thinking profiling cleans or transforms data
- Assuming profiling measures overall script time only
LineProfiler object and add a function my_func for profiling?Solution
Step 1: Recall LineProfiler usage
The correct method to add a function isadd_function()with the function object.Step 2: Check each option
profiler = LineProfiler(); profiler.add_function(my_func) usesadd_function(my_func)correctly. profiler = LineProfiler(my_func); profiler.run() wrongly passes function to constructor. profiler = LineProfiler(); profiler.add(my_func) uses non-existentadd(). profiler = LineProfiler(); profiler.add_function('my_func') passes function name as string, which is incorrect.Final Answer:
profiler = LineProfiler(); profiler.add_function(my_func) -> Option DQuick Check:
Add function with add_function(func) [OK]
- Passing function name as string instead of function object
- Using wrong method like add() instead of add_function()
- Passing function to LineProfiler constructor
profiler.print_stats()?
from line_profiler import LineProfiler
def slow_func():
total = 0
for i in range(3):
total += i
return total
profiler = LineProfiler()
profiler.add_function(slow_func)
profiler.enable()
slow_func()
profiler.disable()
profiler.print_stats()Solution
Step 1: Understand profiling flow
Profiler is created, function added, enabled, function called, then disabled and stats printed.Step 2: Analyze output of print_stats()
print_stats() shows detailed line-by-line timing for added functions, here slow_func, including loop lines.Final Answer:
Detailed timing per line of slow_func showing time spent on the loop lines -> Option CQuick Check:
print_stats() = detailed line timing [OK]
- Thinking print_stats() shows only total time
- Believing enable() must be after add_function() to work
- Confusing run() method requirement
line_profiler:
from line_profiler import LineProfiler
def compute():
s = 0
for i in range(5):
s += i
return s
profiler = LineProfiler()
profiler.add_function('compute')
profiler.enable()
compute()
profiler.disable()
profiler.print_stats()Solution
Step 1: Check add_function argument
add_function requires the function object, not its name as a string.Step 2: Identify error cause
Passing 'compute' as string causes profiler not to track the function properly.Final Answer:
Passing function name as string to add_function instead of function object -> Option AQuick Check:
add_function(func) needs function object [OK]
- Passing function name as string
- Thinking profiler.run() is mandatory
- Assuming import error without checking code
func_a and func_b in your data analysis script. You also want to compare which function is slower line-by-line. Which of the following code snippets correctly profiles both functions and prints their stats separately?Solution
Step 1: Understand profiling multiple functions separately
To compare functions separately, create separate LineProfiler instances for each.Step 2: Analyze each option
profiler = LineProfiler() profiler.add_function(func_a) profiler.add_function(func_b) profiler.enable() func_a() func_b() profiler.disable() profiler.print_stats() profiles both with one profiler but prints combined stats, not separate. profiler_a = LineProfiler() profiler_a.add_function(func_a) profiler_b = LineProfiler() profiler_b.add_function(func_b) profiler_a.enable() func_a() profiler_a.disable() profiler_b.enable() func_b() profiler_b.disable() profiler_a.print_stats() profiler_b.print_stats() profiles separately with two profilers and prints stats separately. profiler = LineProfiler(func_a, func_b) profiler.run() profiler.print_stats() wrongly passes functions to constructor and calls run() without arguments. profiler = LineProfiler() profiler.add_function('func_a') profiler.add_function('func_b') profiler.enable() func_a() func_b() profiler.disable() profiler.print_stats() passes function names as strings, which is incorrect.Final Answer:
profiler_a = LineProfiler() profiler_a.add_function(func_a) profiler_b = LineProfiler() profiler_b.add_function(func_b) profiler_a.enable() func_a() profiler_a.disable() profiler_b.enable() func_b() profiler_b.disable() profiler_a.print_stats() profiler_b.print_stats() -> Option BQuick Check:
Separate profilers for separate stats [OK]
- Passing function names as strings
- Using one profiler for separate stats
- Misusing LineProfiler constructor arguments
Ready to prove it?
Pass the test and this pattern moves unknown
