gpt4 book ai didi

python - 是否有用于 python 的统计分析器?如果没有,我怎么能写一个呢?

转载 作者:IT老高 更新时间:2023-10-28 21:11:13 26 4
gpt4 key购买 nike

我需要随机运行一个 python 脚本,暂停它,获取堆栈回溯,然后取消暂停。我已经用谷歌搜索了一种方法来做到这一点,但我没有看到明显的解决方案。

最佳答案

这里有 statprof module

pip install statprof(或easy_install statprof),然后使用:

import statprof

statprof.start()
try:
my_questionable_function()
finally:
statprof.stop()
statprof.display()

模块中有一些来自 this blog post 的背景知识:

Why would this matter, though? Python already has two built-in profilers: lsprof and the long-deprecated hotshot. The trouble with lsprof is that it only tracks function calls. If you have a few hot loops within a function, lsprof is nearly worthless for figuring out which ones are actually important.

A few days ago, I found myself in exactly the situation in which lsprof fails: it was telling me that I had a hot function, but the function was unfamiliar to me, and long enough that it wasn’t immediately obvious where the problem was.

After a bit of begging on Twitter and Google+, someone pointed me at statprof. But there was a problem: although it was doing statistical sampling (yay!), it was only tracking the first line of a function when sampling (wtf!?). So I fixed that, spiffed up the documentation, and now it’s both usable and not misleading. Here’s an example of its output, locating the offending line in that hot function more accurately:

  %   cumulative      self          
time seconds seconds name
68.75 0.14 0.14 scmutil.py:546:revrange
6.25 0.01 0.01 cmdutil.py:1006:walkchangerevs
6.25 0.01 0.01 revlog.py:241:__init__
[...blah blah blah...]
0.00 0.01 0.00 util.py:237:__get__
---
Sample count: 16
Total time: 0.200000 seconds

I have uploaded statprof to the Python package index, so it’s almost trivial to install: "easy_install statprof" and you’re up and running.

Since the code is up on github, please feel welcome to contribute bug reports and improvements. Enjoy!

关于python - 是否有用于 python 的统计分析器?如果没有,我怎么能写一个呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5616446/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com