代码:

import line_profiler
import sys

def test():
    for i in range(0, 10):
        print( i**2 )
    print("End of the function")


prof = line_profiler.LineProfiler(test) #pass in the function to profile

prof.enable() #start profiling
test()
prof.disable() #stop profiling

prof.dump_stats('test.prof')

prof.print_stats(sys.stdout) #print out the results
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.


运行结果:

python性能分析器:line_profiler_开发





代码2:

from line_profiler import profile

@profile
def test():
    for i in range(0, 10):
        print( i**2 )
    print("End of the function")


test()
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.


运行结果:

kernprof -l -v B02088_02_17.py


python性能分析器:line_profiler_开发_02