代码:
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
运行结果:
代码2:
from line_profiler import profile
@profile
def test():
for i in range(0, 10):
print( i**2 )
print("End of the function")
test()
运行结果:
kernprof -l -v B02088_02_17.py