Message289028
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. |
|
Date |
User |
Action |
Args |
2017-03-05 17:32:38 | steven.daprano | set | recipients:
+ steven.daprano, rhettinger, vstinner, docs@python, Kwpolska |
2017-03-05 17:32:38 | steven.daprano | set | messageid: <1488735158.37.0.824750546179.issue29724@psf.upfronthosting.co.za> |
2017-03-05 17:32:38 | steven.daprano | link | issue29724 messages |
2017-03-05 17:32:37 | steven.daprano | create | |
|