Do Python lambda functions help in reducing the execution times?

Question:

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?

Asked By: bp14

||

Answers:

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.

Answered By: user2357112
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.