函数reduce,lambda,filter

 1 #比较时间差,判断执行有时。
 2 
 3 import time
 4 def panduan(x):
 5     if x%5==0 and x%7==0:
 6         return True
 7     else:
 8         return  False
 9 start_time=time.time()
10 print reduce((lambda x,y:x+y),filter(panduan,range(1001)))
11 end_time=time.time()
12 print end_time-start_time
13 
14 sum=0
15 start_time1=time.time()
16 for i in range(1001):
17     if i%5==0 and i%7==0:
18         sum=sum+i
19 print sum
20 end_time1=time.time()
21 print end_time1-start_time1
22 
23 #执行结果
24 #14210
25 #0.00799989700317
26 #14210
27 #0.00500011444092

两种方法执行1000以内即被5又被7整除的数的和,比较执行所需时间

posted @ 2015-04-17 14:30  听风林  阅读(131)  评论(0编辑  收藏  举报