【python】map list for運転時間長テスト

2385 ワード

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、多態性の実現