2

I'm really confused with functions call speed in Python. First and second cases, nothing unexpected:

%timeit reduce(lambda res, x: res+x, range(1000))

10000 loops, best of 3: 150 μs per loop

def my_add(res, x):
return res + x
%timeit reduce(my_add, range(1000))

10000 loops, best of 3: 148 μs per loop

But third case looks strange for me:

from operator import add
%timeit reduce(add, range(1000))

10000 loops, best of 3: 80.1 μs per loop

At the same time:

%timeit add(10, 100)
%timeit 10 + 100
10000000 loops, best of 3: 94.3 ns per loop
100000000 loops, best of 3: 14.7 ns per loop

So, why the third case gives speed up about 50%?

asked Nov 18, 2013 at 15:39

3 Answers 3

8

add is implemented in C.

>>> from operator import add
>>> add
<built-in function add>
>>> def my_add(res, x):
... return res + x
... 
>>> my_add
<function my_add at 0x18358c0>

The reason that a straight + is faster is that add still has to call the Python VM's BINARY_ADD instruction as well as perform some other work due to it being a function, while + is only a BINARY_ADD instruction.

answered Nov 18, 2013 at 15:43
Sign up to request clarification or add additional context in comments.

Comments

1

The operator module exports a set of efficient functions corresponding to the intrinsic operators of Python. For example, operator.add(x, y) is equivalent to the expression x+y. The function names are those used for special class methods; variants without leading and trailing __ are also provided for convenience.

From Python docs (emphasis mine)

answered Nov 18, 2013 at 15:43

Comments

0

The operator module is an efficient (native I'd assume) implementation. IMHO, calling a native implementation should be quicker than calling a python function.

You could try calling the interpreter with -O or -OO to compile the python core and check the timing again.

answered Nov 18, 2013 at 15:49

2 Comments

All Python code gets compiled to bytecode, -O just strips debugging instructions like assert and if __debug__ blocks.
You're right. I was under the false impression that -O would do the same as gcc. This happens in pypy. Also, this.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.