【发布时间】:2026-01-18 23:55:02
【问题描述】:
已经存在一些讨论使用 cProfile 进行 python 分析的帖子,以及由于以下示例代码中的输出文件 restats 不是纯文本文件而分析输出的挑战.下面的 sn-p 只是来自docs.python.org/2/library/profile 的一个样本,不能直接复现。
import cProfile
import re
cProfile.run('re.compile("foo|bar")', 'restats')
这里有一个讨论:Profile a python script using cProfile into an external file,在 docs.python.org 上有更多关于如何使用 pstats.Stats 分析输出的详细信息(仍然只是一个示例,不可重现) :
import pstats
p = pstats.Stats('restats')
p.strip_dirs().sort_stats(-1).print_stats()
我可能在这里遗漏了一些非常重要的细节,但我真的很想将输出存储在 pandas DataFrame 中并从那里做进一步的分析。
我认为这会很简单,因为在 iPython 中运行 cProfile.run() 的输出看起来相当整洁:
In[]:
cProfile.run('re.compile("foo|bar")'
Out[]:
关于如何以相同格式将其放入 pandas DataFrame 的任何建议?
【问题讨论】: