1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import time def time_cost(func): def _time_cost(*args,**kw): t1=time.time() func(*args,**kw) t2=time.time() return t2-t1 return _time_cost @time_cost def test_add(list_a,huge_list_b): return list_a+huge_list_b @time_cost def test_extend(list_a,huge_list_b): return list_a.extend(huge_list_b) if __name__=='__main__': print '-----big list test-------------' a=[1]*1000 b=['a']*(10**8) print 'add cost:%s seconds'%test_add(a,b) print 'extend cost:%s seconds'%test_extend(a,b) print '-----small list test-------------' a=[1]*1000 b=['a']*(10**2) print 'add cost:%s seconds'%test_add(a,b) print 'extend cost:%s seconds'%test_extend(a,b) |
1 2 3 4 5 6 | -----big list test------------- add cost:1.30500006676 seconds extend cost:0.591000080109 seconds -----small list test------------- add cost:0.0 seconds extend cost:0.0 seconds |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import time def time_cost(func): def _time_cost(*args,**kw): t1=time.time() func(*args,**kw) t2=time.time() return t2-t1 return _time_cost @time_cost def test_add(list_a,huge_list_b): list_a+=huge_list_b return list_a @time_cost def test_extend(list_a,huge_list_b): return list_a.extend(huge_list_b) if __name__=='__main__': print '-----big list test-------------' a=[1]*1000 b=['a']*(10**8) a2=[1]*1000 print '+= cost:%s seconds'%test_add(a,b) print 'extend cost:%s seconds'%test_extend(a2,b) |
1 2 3 | -----big list test------------- += cost:0.506999969482 seconds extend cost:0.510999917984 seconds |
欢迎光临 Odoo中文网|Odoo实施培训 (http://www.chinaodoo.net/) | Powered by Discuz! X3.2 |