【问题标题】:Python cprofile filename:lineno get full pathPython cprofile 文件名:lineno 获取完整路径
【发布时间】:2018-11-16 18:27:19
【问题描述】:

我使用cprofile 来获取高犯,但是filename:lineno 仅列出文件名,但列出文件路径对于快速打开该路径会更有用。特别是如果不同层次结构中可能存在相同的模块名称。

ncalls    tottime    percall    cumtime    percall    filename:lineno(function)
1         0.000       0.000       3.922    display.py:599 (show)

有没有办法把它变成全路径?

【问题讨论】:

    标签: python cprofile


    【解决方案1】:

    如果您是从终端运行,请添加 -o 标志:

    python -m cProfile -o output.data your_script.py ...
    

    然后从控制台:

    import pstats
    ps = pstats.Stats('output.data')
    ps.sort_stats(pstats.SortKey.CUMULATIVE).print_stats(20)
    

    你应该得到如下输出:

    Tue Oct 19 07:17:56 2021    output.data
    
             18646457 function calls (18374990 primitive calls) in 30.760 seconds
    
       Ordered by: cumulative time
       List reduced from 22652 to 20 due to restriction <20>
    
       ncalls  tottime  percall  cumtime  percall filename:lineno(function)
       4321/1    0.037    0.000   30.796   30.796 {built-in method builtins.exec}
            1    0.000    0.000   30.796   30.796 manage.py:1(<module>)
          ...
    

    【讨论】:

      【解决方案2】:

      似乎没有内置方法,但您可以这样做:

      import cProfile                                                    
      import pstats   
                                                         
      p = cProfile.Profile()                      
      s = p.run("1+1")     
      pstats.Stats(s).sort_stats(2).print_stats()
      

      【讨论】:

        【解决方案3】:

        我猜你用“pstats.Stats”类格式化输出,你有:

        stats = Stats(profiler)
        stats.strip_dirs()  # remove this
        

        【讨论】:

        猜你喜欢
        • 2013-07-17
        • 1970-01-01
        • 2011-04-13
        • 1970-01-01
        • 1970-01-01
        • 2011-07-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多