相关资料:
安装依赖
pip install pstats
pip install snakeviz
获取程序运行数据
通过cProfile获取性能分析数据
- cProfile自python2.5以来就是标准版Python解释器默认的性能分析器。
- cProfile是一种确定性分析器,只测量CPU时间,并不关心内存消耗和其他与内存相关联的信息。
def run_1():
"""your function """
pass
def profile_func(func):
import cProfile
file_name = "prof_{}_1.pstat".format(func.__name__)
cProfile.run("{}()".format(func.__name__), file_name)
import pstats
p = pstats.Stats(file_name).sort_stats("cumtime")
# p.print_stats("rqalpha_mod_ricequant_data")
p.print_stats("base_position")
return p
if __name__ == '__main__':
p = profile_func(run_1)