python 性能分析

实例分析



import time
 
#----------------------------------------------------------------------
def fast():
    """"""
    print("I run fast!")
 
#----------------------------------------------------------------------
def slow():
    """"""
    time.sleep(3)
    print("I run slow!")
 
#----------------------------------------------------------------------
def medium():
    """"""
    time.sleep(0.5)
    print("I run a little slowly...")
 
#----------------------------------------------------------------------
def main():
    """"""
    fast()
    slow()
    medium()
 
if __name__ == '__main__':
    main()


python -m cProfile -o output.txt ptest.py



import pstats
p = pstats.Stats("output.txt")
p.strip_dirs().sort_stats(-1).print_stats() # 排序并打印

效果

参考

怎样分析你的代码

Loading Disqus comments...
Table of Contents