2017-8-8 profile cProfile 效率分析 发表于 2017-08-08 | 更新于 2018-02-24 | 分类于 Notes , Python | 评论数: | 阅读次数: 本文字数: 1k | 阅读时长 ≈ 1 分钟 test.py:123456789import osimport sysdef process(filename): print filenamefor (dirpath, dirnames, filenames) in os.walk(sys.argv[1]): for filename in filenames: process(filename) cProfile用法:12345678910111213141516171819202122232425# 生成.pstats分析文档python -m cProfile -o profile.pstats test.py /usr# 排序python -m cProfile -s tottime myscript.py# 查看pstats文档python -m pstats profile.pstats# ?: 查看可用指令;sort cumtime:排序;stats:查看pstats文档-s 选项:'calls' (call count)'cumulative' (cumulative time)'cumtime' (cumulative time)'file' (file name)'filename' (file name)'module' (file name)'ncalls' (call count)'pcalls' (primitive call count)'line' (line number)'name' (function name)'nfl' (name/file/line)'stdname' (standard name)'time' (internal time)'tottime' (internal time) gprof2dot用法:12345678# 安装 gprof2dotpip install gprof2dot# 通过.pstats文档生成相应的dot文档python -m gprof2dot -f pstats profile.pstats# 安装graphviz(centOS系统)sudo yum install graphviz# 输出png文档python -m gprof2dot -f pstats profile.pstats | dot -T png -o profile.png 相关文章 2017-8-27 python 协程加速 2017-10-16 The unit test and mock in python 2017-5-15 数据分析 入门 2017-5-16 从字典中提取子集 2017-5-23 一个逗号(,)引起的BUG 支持小徐?賞一賞! 打赏 微信支付 支付宝