0
0
Data Analysis Pythondata~10 mins

Profiling with line_profiler in Data Analysis Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Profiling with line_profiler
Write Python function
Add @profile decorator
Run script with line_profiler
line_profiler measures time per line
Output shows time spent on each line
Analyze output to find slow lines
Optimize slow lines
Repeat profiling to check improvements
This flow shows how to add profiling to a Python function, run line_profiler to measure time per line, and use the results to optimize code.
Execution Sample
Data Analysis Python
from time import sleep

@profile
def slow_function():
    sleep(0.1)
    total = 0
    for i in range(3):
        total += i
    return total
This code defines a simple function with a sleep and a loop, decorated for line_profiler to measure time spent on each line.
Execution Table
StepLine NumberCode LineTime Spent (s)HitsCumulative Time (s)
13@profile0.000010.0000
24def slow_function():0.000010.0000
35sleep(0.1)0.100110.1001
46total = 00.000010.1001
57for i in range(3):0.000010.1001
68total += i0.000030.1001
79return total0.000010.1001
8-End of function---
💡 Function completes after line 9; total time mostly spent on sleep(0.1) line.
Variable Tracker
VariableStartAfter sleep(0.1)After loop iteration 1After loop iteration 2After loop iteration 3Final
totalundefined00133
iundefinedundefined0122
Key Moments - 3 Insights
Why does the sleep(0.1) line take most of the time?
Because sleep pauses the program for 0.1 seconds, line_profiler shows this as the biggest time consumer (see execution_table row 3).
Why does 'total += i' show zero time even though it runs 3 times?
Each addition is very fast, so total time is too small to show; line_profiler sums time but it rounds to zero here (see execution_table row 6).
What does 'Hits' mean in the profiling output?
'Hits' counts how many times a line runs; for example, the loop line runs once, but the addition line runs 3 times (see execution_table rows 5 and 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which line uses the most time?
ALine 6: total = 0
BLine 5: sleep(0.1)
CLine 8: total += i
DLine 9: return total
💡 Hint
Check the 'Time Spent (s)' column in execution_table row 3.
At which step does the variable 'total' first get a value?
AAfter loop iteration 1
BAfter loop iteration 3
CAfter sleep(0.1)
DAt start
💡 Hint
See variable_tracker row for 'total' after 'After sleep(0.1)'.
If the loop ran 5 times instead of 3, what changes in the execution table?
A'Hits' for line 8 would be 5 instead of 3
B'Time Spent' for sleep(0.1) would increase
C'Hits' for line 7 would be 5
DNo changes in the table
💡 Hint
Look at how 'Hits' counts executions for line 8 in execution_table row 6.
Concept Snapshot
Profiling with line_profiler:
- Add @profile decorator to functions
- Run script with 'kernprof -l script.py'
- Use 'python -m line_profiler script.py.lprof' to see line times
- Shows time spent per line and hit counts
- Helps find slow lines to optimize
Full Transcript
Profiling with line_profiler helps find which lines in a Python function take the most time. You add @profile above the function, run the script with kernprof, then view detailed timing per line. The example function sleeps for 0.1 seconds and sums numbers in a loop. The profiler shows most time is spent on sleep. Variables like 'total' change as the loop runs. Hits count how many times each line runs. This helps focus optimization on slow parts.