Python的pstats模块

本文介绍如何使用Python内置的profile和pstats模块来分析Python代码的性能。通过实例演示了如何记录和解析函数调用的详细信息,包括调用次数、运行时间等关键指标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

profile和pstats是python代码的分析器,可以很客观查看代码的运行质量和使用的资源.在调试程序时有很大的帮助.
1.使用profile分析python的代码

[root@node1 tmp]# vim profile12.py

#!/bin/env python
#!-*- coding:UTF-8 -*-
import profile

def one():                #定义一个one函数

    sum=0
    for i in range(10000):
        sum+=i
    return sum

def two():
    sum=0
    for i in range(100000):
        sum+=i
    return sum

def there():
    sum=0
    for i in range(100000):
        sum+=i
    return sum

if __name__=="__main__":
    profile.run("one()","result")      #将结果保存到result文件中

    profile.run("two()")
    profile.run("there()")
[root@node1 tmp]# python profile12.py
         5 function calls in 0.010 CPU seconds       
   Ordered by: standard name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.003    0.003    0.003    0.003 :0(range)
        1    0.000    0.000    0.000    0.000 :0(setprofile)
        1    0.000    0.000    0.010    0.010 <string>:1(<module>)
        1    0.007    0.007    0.010    0.010 profile12.py:12(two)
        0    0.000             0.000          profile:0(profiler)
        1    0.000    0.000    0.010    0.010 profile:0(two())

  • ncalls:函数调用的次数
  • tottime:函数的总的运行时间,除掉函数中调用子函数的运行时间
  • percall:(第一个 percall)等于tottime/ncalls
  • cumtime:函数及其所有子函数的调用运行的时间,即函数开始调用到返回的时间
  • percall:(第二个 percall)即函数运行一次的平均时间,等于 cumtime/ncalls
  • filename:lineno(function):每个函数调用的具体信息
5 function calls in 0.008 CPU seconds
   Ordered by: standard name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.001    0.001    0.001    0.001 :0(range)
        1    0.000    0.000    0.000    0.000 :0(setprofile)
        1    0.000    0.000    0.008    0.008 <string>:1(<module>)
        1    0.007    0.007    0.008    0.008 profile12.py:18(there)
        0    0.000             0.000          profile:0(profiler)
        1    0.000    0.000    0.008    0.008 profile:0(there())

Thu May  5 17:30:09 2016    result
         5 function calls in 0.001 CPU seconds
   Ordered by: standard name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 :0(range)
        1    0.000    0.000    0.000    0.000 :0(setprofile)
        1    0.000    0.000    0.001    0.001 <string>:1(<module>)
        1    0.001    0.001    0.001    0.001 profile12.py:6(one)
        1    0.000    0.000    0.001    0.001 profile:0(one())
        0    0.000             0.000          profile:0(profiler)
[root@node1 tmp]#


2.使用pstats分析python代码

[root@node1 tmp]# vim profile12.py

#!/bin/env python
#!-*- coding:UTF-8 -*-

import profile,pstats

def one():
    sum=0
    for i in range(10000):
        sum+=i
    return sum

if __name__=="__main__":
    profile.run("one()","result")                #将结果保存到result文件中

    p=pstats.Stats("result")                      #创建一上pstats变量
    p.strip_dirs().sort_stats(-1).print_stats()     #strip_dirs:从所有模块名中去掉无关的路径信息      

    p.strip_dirs().sort_stats("name").print_stats()  #sort_stats():把打印信息按照标准的module/name/line字符串进行排序

    p.strip_dirs().sort_stats("cumulative").print_stats(3)     #print_stats():打印出所有分析信息

[root@node1 tmp]# python profile12.py
Thu May  5 17:54:49 2016    result
         5 function calls in 0.001 CPU seconds
   Ordered by: standard name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 :0(range)
        1    0.000    0.000    0.000    0.000 :0(setprofile)
        1    0.000    0.000    0.001    0.001 <string>:1(<module>)
        1    0.001    0.001    0.001    0.001 profile12.py:6(one)
        1    0.000    0.000    0.001    0.001 profile:0(one())
        0    0.000             0.000          profile:0(profiler)

Thu May  5 17:54:49 2016    result
         5 function calls in 0.001 CPU seconds
   Ordered by: function name
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.001    0.001 <string>:1(<module>)
        1    0.001    0.001    0.001    0.001 profile12.py:6(one)
        1    0.000    0.000    0.001    0.001 profile:0(one())
        0    0.000             0.000          profile:0(profiler)
        1    0.000    0.000    0.000    0.000 :0(range)
        1    0.000    0.000    0.000    0.000 :0(setprofile)

Thu May  5 17:54:49 2016    result
         5 function calls in 0.001 CPU seconds
   Ordered by: cumulative time
   List reduced from 6 to 3 due to restriction <3>
   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.001    0.001    0.001    0.001 profile12.py:6(one)
        1    0.000    0.000    0.001    0.001 profile:0(one())
        1    0.000    0.000    0.001    0.001 <string>:1(<module>)
[root@node1 tmp]#

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wanderer001

ROIAlign原理

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值