python code optimizer/code acceleration/code performance analysis/LineProfiler

Python code runs slowly, and it also obeys the 28th rule, that is, 20% of the code runs more than 80% of the time.

When optimizing code, you can manually use time.time() to time each line of code, but again: not elegant.

So, is there an elegant, simple, and powerful code performance analyzer?

The answer is yes : LineProfiler. line_profiler is a third-party library for Python that functions as a function-based line-by-line code analysis tool. Through this library, the time consumption analysis of the target function (allowing analysis of multiple functions) can be performed, which is convenient for code tuning.

You can use pip to install it, and then follow the official tutorial to use it.

pip install line_profiler

In order to use LineProfiler more conveniently, I wrote a decorator for it. It only needs two lines of code to analyze the detailed time consumption of a function (full sample code link: github link ) :

def performance_test(exit_flag=True):
    def func_layer(func):
        def wrapper(*args, **kwargs):
            from line_profiler import LineProfiler
            lp = LineProfiler()
            lpw = lp(func)  # function need to be analysed
            func_output = lpw(*args, **kwargs)  # the parameters of target function
            lp.print_stats()  # print analyse results
            if exit_flag:
                exit()
            return func_output
        return wrapper
    return func_layer

Add @performance_test(True) in front of the function to be analyzed, and then run the code as usual. The example is as follows:

import performance_test

@performance_test()
def func_test():
    a = [i for i in range(10000)]
    b = sum(a)
    return b

Then run will print out the time-consuming of each line of code in the terminal, which is clear and clear.

Hits indicates how many times it was called; Time indicates the total time spent in the line, in microseconds; Per Hit is the time spent in a single line; %Time is the time ratio, and generally you can see which sentence needs to be optimized by looking at the time ratio .

Timer unit: 1e-06 s

Total time: 0.001297 s
File: /home/longer/Nutstore Files/Longer/working/mf/signal-screener/performance_test.py
Function: func at line 30

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    30                                           @performance_test()
    31                                           def func():
    32         1       1223.0   1223.0     94.3      a = [i for i in range(10000)]
    33         1         73.0     73.0      5.6      b = sum(a)
    34         1          1.0      1.0      0.1      return b

line_profile has many other functions, you can check the official documentation .

Full sample code link: github link

Guess you like

Origin blog.csdn.net/BeiErGeLaiDe/article/details/126987698