profile cProfile 效率分析

功效:对脚本进行简单的效率分析并生成分析图表

test.py:

import os
import sys

def process(filename):
    print filename

for (dirpath, dirnames, filenames) in os.walk(sys.argv[1]):
    for filename in filenames:
        process(filename)

cProfile用法:

# 生成.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用法:

# 安装 gprof2dot
pip 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

作者:Chihwei_hsu
来源:http://chihweihsu.com
Github:https://github.com/HsuChihwei


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值