将 cProfile 结果与 KCacheGrind 结合使用

发布于 2024-08-14 07:07:36 字数 747 浏览 5 评论 0原文

我正在使用 cProfile 来分析我的 Python 程序。根据这篇演讲,我的印象是 KCacheGrind 可以解析并显示 cProfile 的输出。

但是,当我导入文件时,KCacheGrind 仅在状态栏中显示“未知文件格式”错误,并且坐在那里不显示任何内容。

在我的分析统计数据与 KCacheGrind 兼容之前,我需要做一些特殊的事情吗?

...
if profile:
    import cProfile

    profileFileName = 'Profiles/pythonray_' + time.strftime('%Y%m%d_%H%M%S') + '.profile'

    profile = cProfile.Profile()
    profile.run('pilImage = camera.render(scene, samplePattern)')

    profile.dump_stats(profileFileName)
    profile.print_stats()
else:            
    pilImage = camera.render(scene, samplePattern)
...

软件包版本

  • KCacheGrind 4.3.1
  • Python 2.6.2

I'm using cProfile to profile my Python program. Based upon this talk I was under the impression that KCacheGrind could parse and display the output from cProfile.

However, when I go to import the file, KCacheGrind just displays an 'Unknown File Format' error in the status bar and sits there displaying nothing.

Is there something special I need to do before my profiling stats are compatible with KCacheGrind?

...
if profile:
    import cProfile

    profileFileName = 'Profiles/pythonray_' + time.strftime('%Y%m%d_%H%M%S') + '.profile'

    profile = cProfile.Profile()
    profile.run('pilImage = camera.render(scene, samplePattern)')

    profile.dump_stats(profileFileName)
    profile.print_stats()
else:            
    pilImage = camera.render(scene, samplePattern)
...

Package Versions

  • KCacheGrind 4.3.1
  • Python 2.6.2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

墨离汐 2024-08-21 07:07:36

使用 cProfile,您还可以分析现有程序,而无需制作任何单独的分析脚本。只需使用分析器运行程序

python -m cProfile -o profile_data.pyprof script_to_profile.py

,并使用 pyprof2calltree 在 kcachegrind 中打开配置文件数据,其 -k 开关

pyprof2calltree -i profile_data.pyprof -k

会自动在 kcachegrind 中打开数据。例如,对整个 Paster 服务器和 Web 应用程序进行分析,就像

python -m cProfile -o pyprof.out `which paster` serve development.ini

可以使用 easy_install 安装 pyprof2calltree 一样。

With cProfile you can also profile existing programs, without making any separate profiling script. Just run program with profiler

python -m cProfile -o profile_data.pyprof script_to_profile.py

and open profile data in kcachegrind with pyprof2calltree, whose -k switch automatically opens data in kcachegrind

pyprof2calltree -i profile_data.pyprof -k

For example profiling whole paster server and webapp would be done like this

python -m cProfile -o pyprof.out `which paster` serve development.ini

pyprof2calltree can be installed with easy_install.

凌乱心跳 2024-08-21 07:07:36

您可以使用 profilestats.profile 装饰器 ($ pip install profilestats) -- pyprof2calltree 模块的简单包装器(的品牌重塑) lsprofcalltree.py):

from profilestats import profile

@profile
def func():
    # do something here

脚本可以照常运行。 profilestats 创建两个文件:cachegrind.out.profilestatsprofilestats.prof,分别采用 KCachegrind 兼容和 cProfile 格式。

You could use profilestats.profile decorator ($ pip install profilestats) -- a simple wrapper for pyprof2calltree module (rebranding of lsprofcalltree.py):

from profilestats import profile

@profile
def func():
    # do something here

Script can be run as usual. profilestats creates two files: cachegrind.out.profilestats and profilestats.prof in KCachegrind-compatible and cProfile formats correspondingly.

晨与橙与城 2024-08-21 07:07:36

可以使用名为 lscallproftree 的外部模块来完成此操作。

本文介绍了如何操作:CherryPy - CacheGrind

我的结果代码如下所示:

...
if profile:
    import cProfile
    import lsprofcalltree

    profileFileName = 'Profiles/pythonray_' + time.strftime('%Y%m%d_%H%M%S') + '.profile'

    profile = cProfile.Profile()
    profile.run('pilImage = camera.render(scene, samplePattern)')

    kProfile = lsprofcalltree.KCacheGrind(profile)

    kFile = open (profileFileName, 'w+')
    kProfile.output(kFile)
    kFile.close()

    profile.print_stats()    
else:            
    pilImage = camera.render(scene, samplePattern)
...

如果有人知道一种不需要的方法来做到这一点一个外部(即不随 Python 一起提供)模块,我仍然很有兴趣了解它。

It can be done using an external module called lscallproftree

This article explains how: CherryPy - CacheGrind

With my resulting code looking like so:

...
if profile:
    import cProfile
    import lsprofcalltree

    profileFileName = 'Profiles/pythonray_' + time.strftime('%Y%m%d_%H%M%S') + '.profile'

    profile = cProfile.Profile()
    profile.run('pilImage = camera.render(scene, samplePattern)')

    kProfile = lsprofcalltree.KCacheGrind(profile)

    kFile = open (profileFileName, 'w+')
    kProfile.output(kFile)
    kFile.close()

    profile.print_stats()    
else:            
    pilImage = camera.render(scene, samplePattern)
...

If anyone knows a way to do this that doesn't require an external (ie. not shipped with Python) module, I'd still be very interested to hear about it.

生死何惧 2024-08-21 07:07:36

在 KCachegrind/Qcachegrind 中分析代码和可视化结果的 3 种不同方法:

I - CPROFILE

1 - 从 ipython 分析 myfunc()

import cProfile
filename = 'filename.prof'
cProfile.run('myfunc()', filename)

2 - 将文件转换为 shell 中可用的 kcachegrind 文件

sudo pip install pyprof2calltree
pyprof2calltree -i filename.prof -o callgrind.filename.prof

中打开 callgrind.filename.prof

3 - 在 kcachegrind II - 嵌入式 CPROFILE

1 - 配置代码中的几行。

import cProfile
filename = 'filename.prof'
pr = cProfile.Profile()
pr.enable()
# ... lines to profile ...
pr.disable()
pr.dump_stats(filename)

2 - 在 shell 中将文件转换为可用的 kcachegrind 文件

sudo pip install pyprof2calltree
pyprof2calltree -i filename.prof -o callgrind.filename.prof

3 - 在 kcachegrind 中打开 callgrind.filename.prof

III - YAPPI

1 - 从 ipython 或代码中配置 myfunc()

import yappi
filename = 'callgrind.filename.prof'
yappi.set_clock_type('cpu')
yappi.start(builtins=True)
myfunc()
stats = yappi.get_func_stats()
stats.save(filename, type='callgrind')

2 - 在 kcachegrind 中打开 callgrind.filename.prof

3 differents ways to profile your code and visualizing results in KCachegrind/Qcachegrind:

I - CPROFILE

1 - Profile myfunc() from ipython

import cProfile
filename = 'filename.prof'
cProfile.run('myfunc()', filename)

2 - Convert your file to a usable kcachegrind file in your shell

sudo pip install pyprof2calltree
pyprof2calltree -i filename.prof -o callgrind.filename.prof

3 - Open callgrind.filename.prof in kcachegrind

II - EMBEDDED CPROFILE

1 - Profile few lines in your code.

import cProfile
filename = 'filename.prof'
pr = cProfile.Profile()
pr.enable()
# ... lines to profile ...
pr.disable()
pr.dump_stats(filename)

2 - Convert your file to a usable kcachegrind file in your shell

sudo pip install pyprof2calltree
pyprof2calltree -i filename.prof -o callgrind.filename.prof

3 - Open callgrind.filename.prof in kcachegrind

III - YAPPI

1 - Profile myfunc() from ipython or from your code

import yappi
filename = 'callgrind.filename.prof'
yappi.set_clock_type('cpu')
yappi.start(builtins=True)
myfunc()
stats = yappi.get_func_stats()
stats.save(filename, type='callgrind')

2 - Open callgrind.filename.prof in kcachegrind

二智少女 2024-08-21 07:07:36

如果您实际上想做的是查看代码的哪些部分可以优化速度,并且可以在调试器中随机暂停它,此方法有效。这可能会令人惊讶,但您不需要太多堆栈截图。

If what you're actually trying to do is see what parts of your code could be optimized for speed, and you can randomly pause it in the debugger, this method works. It may be surprising, but you don't need very many stackshots.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文