for x in repslist: res.append(abs(x)) return res def listComp(): return [abs(x) for x in repslist] def mapCall(): return list(map(abs, repslist)) def genExpr(): return list(abs(x) for x in repslist) def genFunc(): def gen(): for x in repslist: yield(abs(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])) for test in (forLoop, listComp, mapCall, genExpr, genFunc): (total, result) = timer2.bestoftotal(test, _reps1=5, _reps=1000) print('%-9s : %.5f => [%s..%s] ' % (test.__name__, total, result[0], result[-1]))
def mapCall(): return list(map(abs, repslist)) # Use list here in 3.0 only! # return map(abs, repslist) def genExpr(): return list(abs(x) for x in repslist) # list required to force results def genFunc(): def gen(): for x in repslist: yield abs(x) return list(gen()) # list required to force results print(sys.version) for test in (forLoop, listComp, mapCall, genExpr, genFunc): (total, result) = timer2.bestoftotal(test, _reps1=5, _reps=1000) # Or: # (total, result) = timer2.bestoftotal(test) # (total, result) = timer2.bestof(test, _reps=5) # (total, result) = timer2.total(test, _reps=1000) # (bestof, (total, result)) = timer2.bestof(timer2.total, test, _reps=5) print('%-9s: %.5f => [%s...%s]' % (test.__name__, total, result[0], result[-1]))
start = timer() ret = func(*pargs, **kargs) elapsed = timer() - start if elapsed < best: best = elapsed return (best, ret) def bestoftotal(func, *pargs, **kargs): _reps1 = kargs.pop('_reps1', 5) return min(total(func, *pargs, **kargs) for i in range(_reps1)) import sys, timer2 ... for test in (forLoop, listComp, mapCall, genExpr, genFunc): (total, result) = timer2.bestoftotal(test, _reps1=5, _reps=1000) # 또는: # (total, result) = timer2.bestoftotal(test) # (total, result) = timer2.bestof(test, _reps = 5) # (total, result) = timer2.total(test, _reps = 1000) # (bestof, (total, result)) = timer2.bestof(timer2.total, test, _reps = 5) print('%-9s: %.5f => [%s...%s]' % (test.__name__, total, result[0], result[-1])) from timer2 import total, bestof, bestoftotal total(pow, 2, 1000)[0] # 2 ** 1000, 기본 1,000회 반복 # 0.0029562534118596773 total(pow, 2, 1000, _reps=1000)[0] # 2 ** 1000, 1,000회 반복 # 0.0029733585316193967
# File timesqrt.py import sys, timer2 reps = 10000 repslist = range(reps) # Pull out range list time for 2.6 from math import sqrt # Not math.sqrt: adds attr fetch time def mathMod(): for i in repslist: res = sqrt(i) return res def powCall(): for i in repslist: res = pow(i, .5) return res def powExpr(): for i in repslist: res = i ** .5 return res print(sys.version) for test in (mathMod, powCall, powExpr): elapsed, result = timer2.bestoftotal(test, _reps1=3, _reps=1000) print ('%s: %.5f => %s' % (test.__name__, elapsed, result))
# File timesqrt.py import sys, timer2 reps = 10000 repslist = range(reps) # Pull out range list time for 2.6 from math import sqrt # Not math.sqrt: adds attr fetch time def mathMod(): for i in repslist: res = sqrt(i) return res def powCall(): for i in repslist: res = pow(i, .5) return res def powExpr(): for i in repslist: res = i**.5 return res print(sys.version) for test in (mathMod, powCall, powExpr): elapsed, result = timer2.bestoftotal(test, _reps1=3, _reps=1000) print('%s: %.5f => %s' % (test.__name__, elapsed, result))
res.append(edi(x)) return res def listComp(): return [edi(x) for x in repslist] def mapCall(): return list(map(edi, repslist)) # Use list() here in 3.X only! # return map(abs, repslist) def genExpr(): return list(edi(x) for x in repslist) # list() required to force results def genFunc(): def gen(): for x in repslist: yield edi(x) return list(gen()) # list() required to force results 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]))
def run(): print(sys.version) for test in (forLoop, listComp, mapCall, genExpr, genFunc): (total, result) = timer2.bestoftotal(test, _reps1=5, _reps=1000) # (bestof,(total, result)) = timer.bestoftotal(5, 1000, test) print('%-9s:%.5f => [%s...%s]' % (test.__name__, total, result[0], result[-1]))