reps = 1000 # <== to match pybench (1000 inner) repslist = list(range(reps)) # <== differs from pybench def forLoop(): res = [] for x in repslist: res.append(x ** 2) # <== to match pybench (x **2 all) return res def listComp(): return [x ** 2 for x in repslist] def mapCall(): return list(map((lambda x: x ** 2), repslist)) # list() in 3.X only # return map(((lambda x: x ** 2), repslist) def genExpr(): return list(x ** 2 for x in repslist) # list() in 2.X + 3.X def genFunc(): def gen(): for x in repslist: yield x ** 2 return list(gen()) # list() in 2.X + 3.X print(sys.version) for test in (forLoop, listComp, mapCall, genExpr, genFunc): (bestof, (total, result)) = timer.bestoftotal(5, 1000, test) print ('%-9s: %.5f => [%s...%s]' % (test.__name__, bestof, result[0], result[-1]))
import timer __author__ = 'woody' print(timer.total(1000, pow, 2, 1000)[0]) print(timer.bestof(1000, str.upper, 'spam')[0]) print(timer.bestoftotal(50, 1000, str.upper, 'spam'))
def forLoop(): res = [] for x in repslist: res.append(F(x)) return res def listComp(): return [F(x) for x in repslist] def mapCall(): return list(map(F, repslist)) # list in 3.X only # return map(F, repslist) def genExpr(): return list(F(x) for x in repslist) # list in 2.X + 3.X def genFunc(): def gen(): for x in repslist: yield F(x) return list(gen()) #... print(sys.version) for test in (forLoop, listComp, mapCall, genExpr, genFunc): (bestof, (total, result)) = timer.bestoftotal(5, 1000, test) print ('%-9s: %.5f => [%s...%s]' % (test.__name__, bestof, result[0], result[-1]))
""" timing02 """ import timer t = timer.total(1000, pow, 2, 1000)[0] print("Total time to run pow function: ", t) t = timer.total(1000, str.upper, 'spam') print("Total time to run str function: ", t) t = timer.bestof(1000,pow, 2, 1000000)[0] print("Bestof pow function: ", t) t = timer.bestof(1000, str.upper, 'spam') print("Bestof str function: ", t) t = timer.bestof(50, timer.total, 1000, str.upper, 'spam') print("Bestoftotal str function: ", t) t = timer.bestoftotal(50, 1000, str.upper, 'spam') print("Bestoftotal str function: ", t)