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

标签 python profile stochastic

我需要随机运行一个 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/

相关文章:

iphone - 转换 iPhone 视频 - ffmpeg

python - 如何找出不连续的日期时间索引?如何对连续指数取平均值?

python - 如何将csv文件写入特定文件夹

macos - 如何通过运行脚本让我的所有终端/shell 重新加载它们的环境?

python - 是否有 Django "app"可以管理用户的个人资料?

python - 我是否正确实现了 Milstein 的方法/Euler-Maruyama?

python - 带有粉红噪声的延迟微分方程

python - 比较两个列表中的元素时找到较小的子集

android - 从 android for work 应用程序访问 SD 卡数据

python - 如何在 Python 中求解/拟合几何布朗运动过程?