【问题标题】:Using cProfile results with KCacheGrind将 cProfile 结果与 KCacheGrind 一起使用
【发布时间】:2010-12-26 03:29:31
【问题描述】:

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

软件包版本

  • KCacheGrind 4.3.1
  • Python 2.6.2

【问题讨论】:

  • 只是好奇,谁能指点我这里链接的谈话?好像坏了。

标签: python profiling kcachegrind cprofile


【解决方案1】:

这可以使用一个名为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 提供)模块的方法,我仍然很想听听。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 感谢您,在我的工具箱中拥有多个工具总是很方便。我使用分析器的原因既是为了确认我对时间被占用的地方的怀疑,也是一个熟悉可用工具的机会。我可能会尝试使用完整的配置文件和堆栈镜头。
    • 我不确定这意味着要链接到什么答案,但看起来问题已被迁移,因此锚点不再起作用。
    • 看起来该问题已从programmers.stackexchange.com 中删除。想总结一下链接的答案是什么?
    • @akaihola:是的,有一个小鬼潜伏着。我放了一个不同的链接。
    【解决方案3】:

    您可以使用 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 格式。

    【讨论】:

      【解决方案4】:

      使用 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 可以使用 easy_install 安装。

      【讨论】:

      • 与主题无关,我必须补充一点,对于“paster”,使用 repoze.profiler 过滤器要好得多,它可以直接编写 cachegrind 格式并知道如何将分析限制为 webapp 代码。跨度>
      • 旁注:在基于 Debian 的系统上,将任何 python easy_install 模块添加为包的最简洁方法是:sudo apt-get install python-stdeb 然后pypi-install pyprof2calltree
      • 在 Mac 上,您可以改用 qcachegrindpip install pyprof2calltree && brew install qcachegrind,然后是pyprof2calltree -i profile_data.pyprof && qcachegrind profile_data.pyprof.log
      • @AlexanderLjungberg,你应该添加brew install graphviz
      【解决方案5】:

      在 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
      

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

      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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-21
        • 2021-12-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多