profiling学习笔记

1、time.time()来计时
import time
start_time = time.time()
processing
end_time = time.time()
spend_time=end_time - start_time
开销低,只有总用时

2、cProfile
import cProfile
import requests
cProfile.run()括号里为执行的一段代码
例如:cProfile.run(‘requests.get(“http://tech.glowing.com”)’)
或者
python -m cProfile run.py来执行一段脚本
cProfile.run用起来非常直观,但在实际调试时并不灵活,并且最后打印出来的信息过于冗长。contextmanager可以用来包裹一个代码块,让cProfile的使用更方便。
import cProfile
import pstats
from contextlib import contextmanager
import requests
@contextmanager
def profiling(sortby=‘cumulative’, limit=20):
pr = cProfile.Profile()
pr.enable()
yield pr.disable() # yield 相当于return 加 中断回执。yield后面
的语句会回来继续执行
ps = pstats.Stats(pr).sort_stats(sortby)
ps.print_stats(limit) with profiling(sortby=‘tottime’, limit=5):
requests.get(‘http://tech.glowing.com’)
结果较为清楚,并且只显示最费时的5个函数。

3、line Profiler
结果更加直观,告诉你一个函数中每一行的耗时。
pip install line_profiler
它不能显示子函数运行的详细信息,所以实际可以使用的场景比较有限。

4、定制低开销的Profiler
https://github.com/Glow-Inc/PythonProfilerDemo/blob/master/custom_profiler_demo.py

5 、IPython
%timeit run()

使用IPython %run -p 的命令得到运行过程中每一个函数的耗时

%run -p test.py

%prun 的命令也可以得到运行过程中每一个函数的耗时
%prun -l 3 estimate_pi(1000000)

6、cmd
python -m cProfile test.py 对每个函数的耗时进行分析

kernprof -l -v test.py 得到该函数中某一行代码的耗时情况以及执行次数

7、pytorch
自带了计算模型每个部分耗时的工具,其既可以计算cpu耗时,也可以计算gpu耗时,在pytorch.autograd里面叫profile。

x = torch.randn((1, 1), requires_grad=True)
with torch.autograd.profiler.profile() as prof:
for _ in range(100): # any normal python code, really!
y = x ** 2
y.backward()
print(prof.key_averages().table(sort_by=“self_cpu_time_total”))


Name Self CPU total CPU time avg Number of Calls


mul 32.048ms 32.048ms 200
pow 27.041ms 27.041ms 200
PowBackward0 9.727ms 55.483ms 100
torch::autograd::AccumulateGrad 9.148ms 9.148ms 100
torch::autograd::GraphRoot 691.816us 691.816us 100


torch.autograd.profiler.emit_nvtx(enabled=True, record_shapes=False)
torch.autograd.profiler.load_nvprof(path)
nvprof是用来测试了解并优化CUDA或OpenACC应用程序的性能的分析工具。

参考:
https://blog.csdn.net/weixin_30666753/article/details/97345639
https://fesian.blog.csdn.net/article/details/106713550
https://pytorch.org/docs/stable/autograd.html#profiler
https://blog.csdn.net/yinhuier/article/details/80551268

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值