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
    评论
Pandas profiling是一个用于生成数据分析报告的Python库。它可以帮助我们快速地分析和了解数据集的概况、数据质量、缺失值、异常值等信息。通过使用Pandas profiling,我们可以快速了解数据集的基本特征,并可以生成一个包含各种统计指标、图表和交互式可视化的报告。 要使用pandas-profiling库,首先需要安装它。可以通过在命令行中使用pip命令进行安装。例如,可以运行以下命令来安装pandas-profiling: pip install pandas-profiling 安装完毕后,就可以在Python脚本中导入并使用pandas-profiling库了。可以按照以下基本用法进行使用: import numpy as np import pandas as pd from pandas_profiling import ProfileReport # 创建一个示例数据集 df = pd.DataFrame(np.random.rand(100, 5), columns=["a", "b", "c", "d", "e"]) # 生成报告 profile = ProfileReport(df, title="Pandas Profiling Report") profile.to_file("your_report.html") 上述代码创建了一个包含随机数据的DataFrame,并使用Pandas Profiling生成了一个报告。报告将保存为一个HTML文件,可以在浏览器中打开查看。报告中包含了数据集的各种统计指标、数据质量分析、缺失值分析、异常值分析等内容。 此外,pandas-profiling还支持许多其他功能和参数,比如可以设置报告的标题、生成简化版报告、自定义图表的参数等。可以参考官方文档或者示例代码来进一步了解和使用pandas-profiling库。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Python之pandas-profiling:pandas-profiling库的简介、安装、使用方法之详细攻略](https://blog.csdn.net/qq_41185868/article/details/109710384)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值