Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lambda is slower than function call in python, why

I think lambda is faster than function call, but after testing, I find out that I am wrong. Function call is definitely faster than lambda call.

Can anybody tell me why?

And how to speed up function call in Python?

I'm using Ubuntu 14.04 and Python 2.7.6

>>> timeit('def a():return 222*333 ;a()')
0.08195090293884277
>>> timeit('a=lambda:222*333 ;a()')
0.11071300506591797

>>> timeit('a=lambda: [].extend(range(10)) ;a()')
0.40241098403930664
>>> timeit('a=lambda: [].extend(range(10)) ;a()')
0.4011270999908447
>>> timeit('a=lambda: [].extend(range(10)) ;a()')
0.4064619541168213
>>> timeit('def a(): return [].extend(range(10)) ;a()')
0.07965493202209473
>>> timeit('def a(): return [].extend(range(10)) ;a()')
0.08039593696594238
>>> timeit('def a(): return [].extend(range(10)) ;a()')
0.08103609085083008
>>> timeit('def a(): return [].extend(range(10)) ;a()')
0.08639097213745117

Sorry for my mistake, there is no difference. Correct testing:

>>> timeit('a()', setup="def a():return 222*333")
0.07061290740966797
>>> timeit('a()', setup="a=lambda: 222*333")
0.06967616081237793
like image 458
cherish Avatar asked Oct 24 '14 03:10

cherish


People also ask

Is lambda faster than function in Python?

Being anonymous, lambda functions can be easily passed without being assigned to a variable. Lambda functions are inline functions and thus execute comparatively faster.

Why is lambda function slow?

Without the CPU contention that often affects serverful applications, the primary cause for slow Lambda response time is elevated latency from services that your functions integrate with. In my previous post, we discussed different ways you can track the latency to these external services.

Is lambda function faster than for loop?

The answer is it depends. I have seen cases where using a lambda was slower and where it was faster. I have also seen that with newer updates you get more optimal code.


3 Answers

timeit('def a(): return [].extend(range(10)) ;a()') is not calling a(); The call to a() is part of the definition of a:

In [34]: def a(): return [].extend(range(10)) ;a()

In [35]: import dis

In [36]: dis.dis(a)
  1           0 BUILD_LIST               0
              3 LOAD_ATTR                0 (extend)
              6 LOAD_GLOBAL              1 (range)
              9 LOAD_CONST               1 (10)
             12 CALL_FUNCTION            1
             15 CALL_FUNCTION            1
             18 RETURN_VALUE        
             19 LOAD_GLOBAL              2 (a)
             22 CALL_FUNCTION            0       #<-- a is called
             25 POP_TOP             

If you test the pieces separately, the difference is negligible:

In [24]: %timeit a=lambda: [].extend(range(10))
10000000 loops, best of 3: 68.6 ns per loop

In [25]: %timeit def a2(): return [].extend(range(10))
10000000 loops, best of 3: 68.8 ns per loop

In [22]: %timeit a()
1000000 loops, best of 3: 445 ns per loop

In [23]: %timeit a2()
1000000 loops, best of 3: 442 ns per loop
like image 194
unutbu Avatar answered Oct 13 '22 19:10

unutbu


As pointed out above, your first test only profiles the time it takes to define a. It's actually never called.

Lambda expressions and "normal" functions generate the exact same bytecode, as you can see if you use the dis module:

def a(): return 10
b = lambda: 10

import dis

>>> dis.dis(a)
1           0 LOAD_CONST               1 (10)
            3 RETURN_VALUE
>>> dis.dis(b)
1           0 LOAD_CONST               1 (10)
            3 RETURN_VALUE
like image 12
Max Noel Avatar answered Oct 13 '22 20:10

Max Noel


There's no difference in calling a lambda versus a function. A lambda is just a function created with a single expression and no name.

Say we have two identical functions, one created with a function definition, the other with a lambda expression:

def a():
    return 222*333

b = lambda: 222*333

We see that both are the same type of function object and they both share equivalent byte-code:

>>> type(a)
<class 'function'>
>>> type(b)
<class 'function'>

>>> import dis
>>> dis.dis(a)
  2           0 LOAD_CONST               3 (73926)
              2 RETURN_VALUE
>>> dis.dis(b)
  1           0 LOAD_CONST               3 (73926)
              2 RETURN_VALUE

How can you speed that up? You don't. It's Python. It's pre-optimized for you. There's nothing more for you to do with this code.

Perhaps you could give it to another interpreter, or rewrite it in another language, but if you're sticking to Python, there's nothing more to do now.

Timing it

Here's how I would examine the timings.

Timeit's timeit and repeat both take a callable:

import timeit

Note that timeit.repeat takes a repeat argument as well:

>>> min(timeit.repeat(a, repeat=100))
0.06456905393861234
>>> min(timeit.repeat(b, repeat=100))
0.06374448095448315

These differences are too small to be significant.

like image 8
Russia Must Remove Putin Avatar answered Oct 13 '22 18:10

Russia Must Remove Putin