0
0
Data Analysis Pythondata~5 mins

Profiling with line_profiler in Data Analysis Python

Choose your learning style9 modes available
Introduction

Profiling helps you find which parts of your code take the most time.
Using line_profiler shows time spent on each line, so you can make your code faster.

You want to speed up a slow data analysis script.
You need to find which line in a function is the bottleneck.
You want to compare performance before and after changes.
You are learning how your code runs step-by-step.
You want to optimize a function used many times.
Syntax
Data Analysis Python
from line_profiler import LineProfiler

lp = LineProfiler()
lp.add_function(your_function)
lp_wrapper = lp(your_function)
result = lp_wrapper(*args)
lp.print_stats()

Use lp.add_function() to tell the profiler which functions to check.

Run your function through the profiler wrapper to collect timing data.

Examples
Profile a simple function that sums numbers.
Data Analysis Python
from line_profiler import LineProfiler

def slow_function():
    total = 0
    for i in range(10000):
        total += i
    return total

lp = LineProfiler()
lp.add_function(slow_function)
slow_function = lp(slow_function)
slow_function()
lp.print_stats()
Use the @lp.profile decorator to profile a function.
Data Analysis Python
from line_profiler import LineProfiler

lp = LineProfiler()

@lp.profile

def multiply_numbers(n):
    result = 1
    for i in range(1, n):
        result *= i
    return result

multiply_numbers(1000)
lp.print_stats()
Sample Program

This program profiles two functions: one sums numbers, the other multiplies numbers.
It shows how much time each line takes.

Data Analysis Python
from line_profiler import LineProfiler

def compute_sum(n):
    total = 0
    for i in range(n):
        total += i
    return total

def compute_product(n):
    product = 1
    for i in range(1, n):
        product *= i
    return product

lp = LineProfiler()
lp.add_function(compute_sum)
lp.add_function(compute_product)

compute_sum = lp(compute_sum)
compute_product = lp(compute_product)

compute_sum(10000)
compute_product(1000)

lp.print_stats()
OutputSuccess
Important Notes

Install line_profiler using pip install line_profiler before using it.

Run your script in an environment where line_profiler is available.

Profiling adds overhead, so results show relative times, not exact runtime.

Summary

Profiling with line_profiler helps find slow lines in your code.

Use LineProfiler to add functions and run them wrapped to collect timing.

Print stats to see detailed time spent per line and improve your code speed.