Raymond Hettinger suggests two ways to computer a dot product inĀ https://twitter.com/raymondh/status/597592757037740032. One's a little faster than the other. Can you guess which one it would be?
>>> from timeit import timeit
>>> timeit('sum(x*y for x, y in zip(vec1, vec2))','vec1=range(1,21); vec2=range(11,31)')
1.9215900897979736
>>> timeit('sum(map(operator.mul, vec1, vec2))','import operator; vec1=range(1,21); vec2=range(11,31)')
1.4601068496704102
raymondh on Twitter: "#python tip: Two ways to compute a dot product: sum(map(operator.mul, vec1, vec2)) sum(x*y for x, y in zip(vec1, vec2))"
>>> from timeit import timeit
>>> timeit('sum(x*y for x, y in zip(vec1, vec2))','vec1=range(1,21); vec2=range(11,31)')
1.9215900897979736
>>> timeit('sum(map(operator.mul, vec1, vec2))','import operator; vec1=range(1,21); vec2=range(11,31)')
1.4601068496704102
raymondh on Twitter: "#python tip: Two ways to compute a dot product: sum(map(operator.mul, vec1, vec2)) sum(x*y for x, y in zip(vec1, vec2))"