The basic answer to your question is this, multiplication using *
is handled through C code. In essence if you write something in pure python its going to be slower than the C implementation, let me give you an example.
The operator.mul
function is implemented in C, but a lambda
is implemented in Python, we're going to try to find the product of all the numbers in an array using functools.reduce
and we are going to use two cases, one using operator.mul
and another using a lambda
which both do the same thing (on the surface):
from timeit import timeit
setup = """
from functools import reduce
from operator import mul
"""
print(timeit('reduce(mul, range(1, 10))', setup=setup))
print(timeit('reduce(lambda x, y: x * y, range(1, 10))', setup=setup))
Output:
1.48362842561
2.67425475375
operator.mul
takes less time, as you can see.