[issue29724] Itertools docs propose a harmful “speedup” without any explanation

Steven D'Aprano report at bugs.python.org
Sun Mar 5 12:32:38 EST 2017


Steven D'Aprano added the comment:

On my computer, running Python 3.5 and continuing to do other tasks while the tests are running, I get a reproducible 5% speedup by using the "default values" trick. Here's my code:

import operator
def dotproduct(vec1, vec2):
    return sum(map(operator.mul, vec1, vec2))

def dotproduct2(vec1, vec2, sum=sum, map=map, mul=operator.mul):
    return sum(map(mul, vec1, vec2))

setup = 'from __main__ import a, b, dotproduct, dotproduct2'
from random import random as r
a = [[r(), r(), r()] for i in range(10000)]
b = [[r(), r(), r()] for i in range(10000)]
t1 = Timer('for v1, v2 in zip(a, b): dotproduct(v1, v2)', setup)
t2 = Timer('for v1, v2 in zip(a, b): dotproduct2(v1, v2)', setup)


I then ran and compared 

min(t1.repeat(number=200, repeat=10))
min(t2.repeat(number=200, repeat=10))

a few times while reading email and doing local editing of files. Normal desktop activity. Each time, t2 (the dotproduct with the micro-optimizations) was about 5% faster.

Victor will probably tell me I'm micro-benchmarking this the wrong way, so to satisfy him I did one more run:

py> import statistics
py> d1 = t1.repeat(number=200, repeat=10)
py> d2 = t2.repeat(number=200, repeat=10)
py>
py> statistics.mean(d1); statistics.stdev(d1)
5.277554708393291
0.15216686556059497
py> statistics.mean(d2); statistics.stdev(d2)
4.929395379964262
0.05397586490809523


So I'm satisfied that this trick gives a real, if small, speed up for at least the example given. YMMV.

----------
nosy: +haypo
type:  -> performance

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue29724>
_______________________________________


More information about the Python-bugs-list mailing list