延长处理对python性能的影响

Python代码 复制代码  收藏代码
  1. #! /bin/usr/env python   
  2. # -*- coding:utf-8 -*-   
  3.   
  4. import time   
  5.   
  6. #统计方法执行的时间   
  7. def count_time(func):   
  8.     def wrap(*args):   
  9.         start = time.time()   
  10.         func(*args)   
  11.         end = time.time()   
  12.         print "func:%s  time:(%0.3f ms)" % (func.func_name, (end-start) * 1000)   
  13.     return wrap   
  14.   
  15. #key不存在的时候  
  16. @count_time  
  17. def not_exists_use_try(max):   
  18.     dict_list = {"do_something":"...."}   
  19.     for item in range(0, max):   
  20.         try:   
  21.             dict_list["not_exists"]   
  22.         except:   
  23.             pass  
  24.   
  25. #key存在的时候  
  26. @count_time  
  27. def exists_use_try(max):   
  28.     dict_list = {"do_something":"...."}   
  29.     for item in range(0, max):   
  30.         try:   
  31.             dict_list["do_something"]   
  32.         except:   
  33.             pass  
  34.   
  35. #key不存在的时候并使用Exception  
  36. @count_time  
  37. def not_exists_use_try_except(max):   
  38.     dict_list = {"do_something":"...."}   
  39.     for item in range(0, max):   
  40.         try:   
  41.             dict_list["not_exists"]   
  42.         except Exception, e:   
  43.             pass  
  44.   
  45. #key存在的时候并使用Exception  
  46. @count_time  
  47. def exists_use_try_except(max):   
  48.     dict_list = {"do_something":"...."}   
  49.     for item in range(0, max):   
  50.         try:   
  51.             dict_list["do_something"]   
  52.         except Exception, e:   
  53.             pass  
  54.   
  55. #使用防御性编码  
  56. @count_time  
  57. def not_use_try(max):   
  58.     dict_list = {"do_something":"...."}   
  59.     for item in range(0, max):   
  60.         if "not_exists" in dict_list :   
  61.             pass  
  62.         else:   
  63.             pass  
  64.   
  65. def run(max):   
  66.     print "max:%s" % max   
  67.     not_exists_use_try(max)   
  68.     not_exists_use_try_except(max)   
  69.     exists_use_try(max)   
  70.     exists_use_try_except(max)   
  71.     not_use_try(max)   
  72.   
  73. if __name__ == "__main__":   
  74. #100   
  75.     run(100)   
  76. #1,000   
  77.     run(1000)   
  78. #10,000   
  79.     run(10000)   
  80. #100,000   
  81.     run(100000)   
  82. #1,000,000   
  83.     run(1000000)   
  84. #10,000,000   
  85.     run(10000000)  
#! /bin/usr/env python
# -*- coding:utf-8 -*-

import time

#统计方法执行的时间
def count_time(func):
    def wrap(*args):
        start = time.time()
        func(*args)
        end = time.time()
        print "func:%s  time:(%0.3f ms)" % (func.func_name, (end-start) * 1000)
    return wrap

#key不存在的时候
@count_time
def not_exists_use_try(max):
    dict_list = {"do_something":"...."}
    for item in range(0, max):
        try:
            dict_list["not_exists"]
        except:
            pass

#key存在的时候
@count_time
def exists_use_try(max):
    dict_list = {"do_something":"...."}
    for item in range(0, max):
        try:
            dict_list["do_something"]
        except:
            pass

#key不存在的时候并使用Exception
@count_time
def not_exists_use_try_except(max):
    dict_list = {"do_something":"...."}
    for item in range(0, max):
        try:
            dict_list["not_exists"]
        except Exception, e:
            pass

#key存在的时候并使用Exception
@count_time
def exists_use_try_except(max):
    dict_list = {"do_something":"...."}
    for item in range(0, max):
        try:
            dict_list["do_something"]
        except Exception, e:
            pass

#使用防御性编码
@count_time
def not_use_try(max):
    dict_list = {"do_something":"...."}
    for item in range(0, max):
        if "not_exists" in dict_list :
            pass
        else:
            pass

def run(max):
    print "max:%s" % max
    not_exists_use_try(max)
    not_exists_use_try_except(max)
    exists_use_try(max)
    exists_use_try_except(max)
    not_use_try(max)

if __name__ == "__main__":
#100
    run(100)
#1,000
    run(1000)
#10,000
    run(10000)
#100,000
    run(100000)
#1,000,000
    run(1000000)
#10,000,000
    run(10000000)

 通过对上面的实验程序的3次运行,采样结果如下:

Python代码 复制代码  收藏代码
  1. max:100  
  2. func:not_exists_use_try  time:(0.110 ms)   
  3. func:not_exists_use_try_except  time:(0.110 ms)   
  4. func:exists_use_try  time:(0.012 ms)   
  5. func:exists_use_try_except  time:(0.011 ms)   
  6. func:not_use_try  time:(0.009 ms)   
  7.   
  8. max:1,000  
  9. func:not_exists_use_try  time:(0.941 ms)   
  10. func:not_exists_use_try_except  time:(1.058 ms)   
  11. func:exists_use_try  time:(0.091 ms)   
  12. func:exists_use_try_except  time:(0.091 ms)   
  13. func:not_use_try  time:(0.063 ms)   
  14.   
  15. max:10,000  
  16. func:not_exists_use_try  time:(10.341 ms)   
  17. func:not_exists_use_try_except  time:(10.869 ms)   
  18. func:exists_use_try  time:(0.879 ms)   
  19. func:exists_use_try_except  time:(0.904 ms)   
  20. func:not_use_try  time:(0.616 ms)   
  21.   
  22. max:100,000  
  23. func:not_exists_use_try  time:(95.245 ms)   
  24. func:not_exists_use_try_except  time:(109.051 ms)   
  25. func:exists_use_try  time:(9.277 ms)   
  26. func:exists_use_try_except  time:(9.290 ms)   
  27. func:not_use_try  time:(7.086 ms)   
  28.   
  29. max:1,000,000  
  30. func:not_exists_use_try  time:(932.254 ms)   
  31. func:not_exists_use_try_except  time:(1088.768 ms)   
  32. func:exists_use_try  time:(110.238 ms)   
  33. func:exists_use_try_except  time:(104.085 ms)   
  34. func:not_use_try  time:(85.284 ms)   
  35.   
  36. max:10,000,000  
  37. func:not_exists_use_try  time:(9292.667 ms)   
  38. func:not_exists_use_try_except  time:(10858.698 ms)   
  39. func:exists_use_try  time:(1037.037 ms)   
  40. func:exists_use_try_except  time:(1008.167 ms)   
  41. func:not_use_try  time:(812.829 ms)  
max:100
func:not_exists_use_try  time:(0.110 ms)
func:not_exists_use_try_except  time:(0.110 ms)
func:exists_use_try  time:(0.012 ms)
func:exists_use_try_except  time:(0.011 ms)
func:not_use_try  time:(0.009 ms)

max:1,000
func:not_exists_use_try  time:(0.941 ms)
func:not_exists_use_try_except  time:(1.058 ms)
func:exists_use_try  time:(0.091 ms)
func:exists_use_try_except  time:(0.091 ms)
func:not_use_try  time:(0.063 ms)

max:10,000
func:not_exists_use_try  time:(10.341 ms)
func:not_exists_use_try_except  time:(10.869 ms)
func:exists_use_try  time:(0.879 ms)
func:exists_use_try_except  time:(0.904 ms)
func:not_use_try  time:(0.616 ms)

max:100,000
func:not_exists_use_try  time:(95.245 ms)
func:not_exists_use_try_except  time:(109.051 ms)
func:exists_use_try  time:(9.277 ms)
func:exists_use_try_except  time:(9.290 ms)
func:not_use_try  time:(7.086 ms)

max:1,000,000
func:not_exists_use_try  time:(932.254 ms)
func:not_exists_use_try_except  time:(1088.768 ms)
func:exists_use_try  time:(110.238 ms)
func:exists_use_try_except  time:(104.085 ms)
func:not_use_try  time:(85.284 ms)

max:10,000,000
func:not_exists_use_try  time:(9292.667 ms)
func:not_exists_use_try_except  time:(10858.698 ms)
func:exists_use_try  time:(1037.037 ms)
func:exists_use_try_except  time:(1008.167 ms)
func:not_use_try  time:(812.829 ms)

 观察上面的采样结果得知:
一、程序执行时间随着执行的次数同比递增增长。
二、其中使用try...except,Exception的方式会比使用try...except的方式稍花时间,但这点时间可以忽略不计。
三、其中当使用try方式时发生异常比使用try方式时无异常花费时间约10倍。
四、使用防御性方式编码在这几种方式中最花费时间最少。

三、总结

以上数据会根据程序执行环境的不同而得出不同的采样结果,从上面的采样数据结果来看,执行次数在10,000,000级别时候才有明显的延时,抛开性能影响的层面,作为靠谱的程序员,应该采取防御性的方式编码,而不应该将错误的处理都丢给系统,这样的好处明显就是性能的提升,同时也加强了程序的可读性。

 

 

 

补充测试:

Python代码 复制代码  收藏代码
  1. @count_time  
  2. def exists_not_use_try(max):   
  3.     dict_list = {"do_something":"...."}   
  4.     for item in range(0, max):   
  5.         if "do_something" in dict_list :   
  6.             dict_list["do_something"]   
  7.         else:   
  8.             pass  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值