【python】map list for 运行时长测试
import time,sys reps=1000 size=10000 def tester(func,*args): starttime=time.time() for i in range(reps): func(*args) spendtime=time.time()-starttime return spendtime def forstst(): res=[] for x in range(size): res.append(abs(x)) def liststst(): res=[abs(x) for x in range(size)] def mapfunc(): res=map(abs,range(size)) def gen(): res=list(abs(x) for x in range(size)) print sys.version tests=(forstst,liststst,mapfunc,gen) for testfunc in tests: print testfunc.__name__.ljust(20),'=>',tester(testfunc) row_input()
1、测试运行时长
2、多态性的实现