33

It is understood that Python lambda functions help in creating anonymous functions. These can be used in other functions like map(), reduce(), filter() and key() in sorting functions. It can also be used to demonstrate and utilise lexical closures.

What I would like to specifically know here is, do lambda functions have a specific advantage over regular functions in terms of their execution times, considering all the other factors to be unchanged?

As I am new to Python, I have tried to understand them by analogously comparing them with the inline functions of C++. Inline functions, as I understand from C++, are useful in saving time as they do not require the necessary "housekeeping tasks" concerned with context switching that occur during function calls and jumps.
Do Python Lambda functions provide with such similar advantages over regular functions?

Some relevant posts that I found useful but not necessarily helpful for my question: Why are Python lambdas useful? Why use lambda functions?

2
  • Yes if you just want to return something from the function you should prefer lambda. Commented Dec 1, 2018 at 8:00
  • Currently, accepting user2357112's answer until a better explanation is put forward. Thanks everyone! Commented Dec 7, 2018 at 13:17

1 Answer 1

48

No. The function objects generated by lambda behave exactly like those generated by def. They do not execute any faster. (Also, inline in modern C++ is no longer a directive telling the compiler to inline a function, and has very little to do with inlining.)

If you want, you can take a look at the bytecode disassembly for a lambda and an equivalent def:

import dis

dis.dis(lambda x: x + 2)

print()
def f(x): return x + 2

dis.dis(f)

Output:

  3           0 LOAD_FAST                0 (x)
              3 LOAD_CONST               1 (2)
              6 BINARY_ADD
              7 RETURN_VALUE

  6           0 LOAD_FAST                0 (x)
              3 LOAD_CONST               1 (2)
              6 BINARY_ADD
              7 RETURN_VALUE

No difference. You can also time them:

import timeit

def f(x): return x + 2
g = lambda x: x + 2

print(timeit.timeit('f(3)', globals=globals()))
print(timeit.timeit('g(3)', globals=globals()))

Output:

0.06977041810750961
0.07760106027126312

The lambda actually took longer in this run. (There seems to be some confusion in the comments about whether we're timing enough work to be meaningful. timeit wraps the timed statement in a million-iteration loop by default, so yes, we are.)

Before you ask, no, lambda has no performance disadvantage over def either. The winner of the above race is basically up to luck. lambda and def do have a significant disadvantage over avoiding the use of a callback function entirely, though. For example, map-with-lambda has a significant performance penalty relative to list comprehensions:

import timeit

print(timeit.timeit('list(map(lambda x: x*x, range(10)))'))
print(timeit.timeit('[x*x for x in range(10)]'))

Output:

1.5655903220176697
0.7803761437535286

Whether lambda or def, Python functions are expensive to call.

Sign up to request clarification or add additional context in comments.

10 Comments

"map-with-lambda has a significant performance penalty relative to list comprehensions". We don't say that enough
@user2357112 Thanks for your reply. The bytecode disassembly and timing outputs are revealing. My current understanding is: lambdas are not particularly advantageous over def functions. They help to make the code more readable(if not always, at least sometimes). It is also a legacy of functional programming choices - Thanks!
@Jean-FrançoisFabre: maybe, although just looking at the two lines of code side-by-side I prefer the list comprehension regardless of performance. So there's normally no need to talk about it!
@Jean-FrançoisFabre: agreed, and specifically I'd say that list+map+lambda is there to prove that there's something else (probably one or more functional languages) that you're more expert in than you are in Python.
@Jared: FWIW the reason an expert-in-another-language may fail to realise that is that in some languages a list comprehension is just syntactic sugar for the list+map+lambda equivalent.
|

Your Answer

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.