Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将cProfile结果与KCacheGrind一起使用_Python_Profiling_Kcachegrind_Cprofile - Fatal编程技术网

Python 将cProfile结果与KCacheGrind一起使用

Python 将cProfile结果与KCacheGrind一起使用,python,profiling,kcachegrind,cprofile,Python,Profiling,Kcachegrind,Cprofile,我正在使用cProfile评测我的Python程序。基于此,我的印象是KCacheGrind可以解析和显示cProfile的输出 但是,当我要导入文件时,KCacheGrind只是在状态栏中显示一个“未知文件格式”错误,而在那里什么也没有显示 在我的评测统计数据与KCacheGrind兼容之前,我需要做一些特殊的事情吗 ... if profile: import cProfile profileFileName = 'Profiles/pythonray_' + time.s

我正在使用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

可以使用名为

本文介绍了如何:

我的结果代码如下所示:

...
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未附带)模块的方法,我还是很有兴趣了解它。

如果您真正想做的是查看代码的哪些部分可以进行速度优化,并且您可以在调试器中随机暂停它。这可能会令人惊讶,但您不需要太多堆栈快照。

您可以使用decorator(
$pip install profilestats
)——一个简单的模块包装器(重新命名
lsprofcalltree.py
):


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

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

python -m cProfile -o profile_data.pyprof script_to_profile.py
并使用pyprof2calltree打开kcachegrind中的配置文件数据,其-k开关自动打开kcachegrind中的数据

pyprof2calltree -i profile_data.pyprof -k
例如,分析整个贴纸服务器和webapp将这样做

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

pyprof2calltree可以轻松安装。

在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
sudo pip install pyprof2calltree
pyprof2calltree -i filename.prof -o callgrind.filename.prof
3-在kcachegrind中打开callgrind.filename.prof

pyprof2calltree -i profile_data.pyprof -k
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
sudo pip install pyprof2calltree
pyprof2calltree -i filename.prof -o callgrind.filename.prof
3-在kcachegrind中打开callgrind.filename.prof

pyprof2calltree -i profile_data.pyprof -k
III-雅皮 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

谢谢,在我的工具箱中有多个工具总是很方便的。我使用探查器的原因既是为了证实我对占用时间地点的怀疑,也是为了有机会熟悉可用的工具。我可能会继续尝试完整配置文件和堆栈快照。我不确定这是要链接到什么答案,但看起来问题已被迁移,因此锚不再起作用。看起来问题已从programmers.stackexchange.com中删除。想总结一下链接的答案是什么吗?@akaihola:是的,有一个小精灵潜伏着。我添加了一个不同的链接。与主题无关,我必须添加一个,对于“粘贴器”,最好使用repose.profiler过滤器,它可以直接写入cachegrind格式,并且知道如何将分析限制为仅限于webapp代码。旁注:在基于Debian的系统上,将任何python easy_install模块作为软件包添加的最干净的方法是:
sudo apt get install python stdeb
然后
pypi install pyprof2calltree
。在Mac上,您可以使用
qcachegrind
pip安装pyprof2calltree和&brew安装qcachegrind
,然后
pyprof2calltree-i profile\u data.pyprof和&qcachegrind profile\u data.pyprof.log
@alexanderjungberg,你应该添加
brew安装graphviz
好奇,有人能给我指一下这里链接的话题吗?它好像坏了。
pyprof2calltree -i profile_data.pyprof -k