https://github.com/pyutils/line_profiler

line-profiler


Introduction

Line profiler is a Python library that allows developers to profile the execution time of each line of code in their program. The library is used to optimize the performance of the code and identify which lines of code are taking the longest time to execute.

How it Works

The library works by adding timing instrumentation to each line of code in the program and running the program to collect timing data. This collected data is then analyzed and presented to the developer in a report, which can be used to optimize the program's performance.

Line profiler can be used on almost any Python program, from small scripts to large-scale applications. It can be used to identify performance bottlenecks in CPU-bound or I/O-bound code, and can be used to optimize code that is running on a single processor or on a distributed system.

How to Use

To use line profiler, developers only need to install the library using pip and add a decorator to the function they want to profile. The decorator will automatically profile the function and generate a report, making it very easy to use. The library not only supports line profiling but also other profiling methods such as memory profiling and function profiling. These can be used to identify memory leaks and other issues with memory usage in Python programs.

Example

In a simple example, a developer may want to optimize a function that calculates the sum of a large list of numbers. Using line profiler, the developer can quickly identify which parts of the function are taking the longest to execute and optimize those parts of the code. For example, the developer may find that the function is spending the most time in a loop that adds each number in the list. The developer can then optimize this loop to use a more efficient algorithm, resulting in a faster execution time for the entire function.

Code Example

from line_profiler import LineProfiler

def my_function():
    numbers = [i for i in range(1000000)]
    total = 0
    for number in numbers:
        total += number
    return total

if __name__ == '__main__':
    profiler = LineProfiler()
    profiler.add_function(my_function)
    profiler.enable_by_count()
    my_function()
    profiler.print_stats()

This example shows how to use line profiler to profile a simple Python function that sums a list of numbers. The LineProfiler class is imported from the line_profiler library, and the my_function function is defined. In the if __name__ == '__main__': block, the LineProfiler object is created and the my_function function is added to the profiler using the add_function method. The enable_by_count method is called to enable the profiler, and the my_function function is executed. Finally, the print_stats method is called to print the profiling report.

When the my_function function is executed, line profiler will profile each line of code and generate a report showing the execution time of each line. The print_stats method is used to print the profiling report to the console.

The output of this example is shown in the original document, in the "Example" section.

Here is the expected output when running the example code:

Timer unit: 1e-06 s

Total time: 0.209901 s
File: example.py
Function: my_function at line 4

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     4                                           def my_function():
     5      1001       5537.0      5.5      2.6      numbers = [i for i in range(1000000)]
     6         1     205195.0 205195.0     97.4      total = 0
     7   1000001     398169.0      0.4      0.2      for number in numbers:
     8   1000000     144784.0      0.1      0.1          total += number
     9         1          5.0      5.0      0.0      return total

This output shows the total time taken to execute the my_function function and the time taken per line of code. The Line #, Hits, Time, Per Hit, and % Time columns provide detailed information about the execution time of each line of code. The output shows that the loop on line 7 took the most time to execute, accounting for 97.4% of the total time. This information can be used to optimize the function and improve its performance.