from time import time
t = time ()
abbreviations = ['cf.',' e.g.','ex.','etc.','fig.','i.e.','Mr.','vs.']
for i in range (1000000):
for w in (' Mr.','Hat1','is','chasing', 'the','black', ' cat', '.'):
if w in abbreviations:
#if w[-1] __ '.' and w in abbreviations:
pass
print ('total run time:')
total run time:
print (time()-t)
2.2108657360076904
t = time()
for i in range (1000000):
for w in (' Mr.','Hat1','is','chasing', 'the','black', ' cat', '.'):
# if w in abbreviations:
if w[-1] == '.' and w in abbreviations:
pass
print ('total run time:')
total run time:
print (time()-t)
1.7705583572387695
def fib ():
a,b=0,1
while True:
yield a
a, b =b, a+b
from itertools import islice
print (list (islice (fib () , 5)))
[0, 1, 1, 2, 3]
Lazy evaluation
并不是一个很大、很新鲜的话题,但古人云“不积跬步无以至千里",
小小的改进便能写出更为优化的代码,何乐而不为呢?