代码之家  ›  专栏  ›  技术社区  ›  Adam Luchjenbroers

使用kcachegrind的cprofile结果

  •  52
  • Adam Luchjenbroers  · 技术社区  · 15 年前

    我正在使用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.62
    5 回复  |  直到 15 年前
        1
  •  88
  •   Mikael Lepistö    14 年前

    使用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可以通过简单的安装来安装。

        2
  •  17
  •   jfs    15 年前

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

    from profilestats import profile
    
    @profile
    def func():
        # do something here
    

    脚本可以像往常一样运行。 profilestats 创建两个文件: cachegrind.out.profilestats profilestats.prof 在kcachegrind兼容和cprofile格式相应。

        3
  •  7
  •   Adam Luchjenbroers    15 年前

    它可以使用一个称为 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一起提供)模块的方法,我还是会非常感兴趣的。

        4
  •  6
  •   Community CDub    8 年前

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

        5
  •  5
  •   Axel Borja    8 年前

    在kcachegrind/qcachegrind中有3种不同的方法来分析代码和可视化结果:

    I型剖面图

    1-从IPython配置myfunc()。

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

    2-将您的文件转换为外壳中可用的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-将您的文件转换为外壳中可用的kcachegrind文件

    sudo pip安装pyprof2calltree
    pyprof2calltree-i文件名.prof-o callgrind.filename.prof
    

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

    雅皮布

    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

    推荐文章