我正在使用cProfile来分析我的Python程序。基于this talk,我的印象是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)
...包版本
发布于 2009-12-13 18:40:35
这可以使用一个名为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一起提供的)模块,我仍然很有兴趣了解它。
发布于 2010-08-25 06:26:26
使用cProfile,您还可以分析现有程序,而无需创建任何单独的分析脚本。只需使用profiler运行程序
python -m cProfile -o profile_data.pyprof script_to_profile.py使用pyprof2calltree在kcachegrind中打开配置文件数据,它的-k开关会自动在kcachegrind中打开数据
pyprof2calltree -i profile_data.pyprof -k例如,分析整个paster服务器和webapp的过程如下
python -m cProfile -o pyprof.out `which paster` serve development.ini可以与easy_install一起安装pyprof2calltree。
发布于 2010-03-29 06:01:10
您可以使用profilestats.profile装饰器($ pip install profilestats) -- pyprof2calltree模块(lsprofcalltree.py的更名)的简单包装器:
from profilestats import profile
@profile
def func():
# do something here脚本可以像往常一样运行。profilestats创建了两个文件:与KCachegrind兼容的cachegrind.out.profilestats和profilestats.prof,以及相应的cProfile格式。
https://stackoverflow.com/questions/1896032
复制相似问题