Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do Python lambda functions help in reducing the execution times?

Tags:

python

lambda

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?

like image 908
bp14 Avatar asked Dec 01 '18 07:12

bp14


People also ask

Are Python lambda functions faster?

Lambda functions are inline functions and thus execute comparatively faster.

How do I reduce lambda execution time?

If we need to reduce the Lambda execution time, we can try increasing memory (and by extension, CPU) to process it faster. However, when we try to increase the memory for a function past a certain limit, it won't improve the execution time as AWS currently offers a maximum of 2 cores CPU.

What is the advantage of lambda function in Python?

Lambda functions allow you to create small, single-use functions that can save time and space in your code. They ares also useful when you need to call a function that expects a function as an argument for a callback such as Map() and Filter() .

How does lambda function increase execution time?

You cannot increase the runtime to more than 15 minutes. The AWS Lambda limit page states the Function timeout is 900 seconds (15 minutes) . If you need more than 15 minutes of execution time you have to look at other services. You could have a look if AWS Batch would suit your needs.


1 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.

like image 62
user2357112 supports Monica Avatar answered Sep 19 '22 15:09

user2357112 supports Monica