我使用的cProfile档我的蟒蛇的程序。根据 这次谈话 我的印象是,某些回答呈可以分析并显示出从cProfile.

然而,当我去进口文件时,某些回答呈只是显示一个'未知的文件的格式错误在状态吧,坐在那里显示什么。

是不是有什么特别我需要做之前,我的分析统计数据与某些回答呈?

...
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)
...

包装版本

  • 某些回答呈4.3.1
  • 蟒蛇2.6.2
有帮助吗?

解决方案 3

它可以通过使用一个外部模块叫 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)
...

如果有人知道一种方法来这样做,不需要外部(ie。不附带Python)模块,我还要非常感兴趣听到关于它.

其他提示

与cProfile你还可以分析现有方案,没有进行任何独立的分析脚本。只是运行程序与分析器

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

和打开档案数据在某些回答呈与pyprof2calltree,其k开关的自动打开的数据在某些回答呈

pyprof2calltree -i profile_data.pyprof -k

例如分析整个贴服务器和网络应用程序会做像这样

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

pyprof2calltree可以安装与执.

你可以用 profilestats.profile 装饰($ pip install profilestats)--一个简单的包装 pyprof2calltree 模块(品牌的 lsprofcalltree.py):

from profilestats import profile

@profile
def func():
    # do something here

脚本可以作为常。 profilestats 创建两个文件: cachegrind.out.profilestatsprofilestats.prof 在某些回答呈兼容和cProfile格式相应地.

如果你实际上想要做的就是看到哪部分的你的代码可以优化速度,并且可以随意暂停它在调试器, 这个方法工作.它可能是令人惊讶,但你不需要很多stackshots.

3个不同的方法来分析代码和可视化的结果在某些回答呈/Qcachegrind:

我-CPROFILE

1调myfunc()从ipython

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

2-把你的文件以一种可使用的某些回答呈文件在你的外壳

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

3-打开callgrind.文件名。教授在某些回答呈

II-嵌入CPROFILE

1调的几行你的代码。

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

2-把你的文件以一种可使用的某些回答呈文件在你的外壳

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

3-打开callgrind.文件名。教授在某些回答呈

III-YAPPI

1调myfunc()从ipython或从你的代码

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-打开callgrind.文件名。教授在某些回答呈

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top