Speed of function vs lambda calls in python

In summary, the difference between speed of function and lambda calls in Python is that lambda calls are faster due to not needing to create a new function object. The speed of function and lambda calls can greatly affect a Python program's performance, with frequent and lengthy function calls slowing it down and lambda calls potentially improving it. Lambda calls can be used as a replacement for regular functions in certain scenarios, but not all, as they lack certain functionality. Potential disadvantages of using lambda calls include decreased readability and difficulty with debugging, as well as limited functionality for certain use cases. To determine the speed of function and lambda calls in a Python program, one can use tools such as the timeit module or profiling tools like cProfile or line_profiler.
  • #1
ergospherical
974
1,277
An observation I made earlier- something like
Python:
def f(...):
    ...
    return ...

def g:
    ... = f(...)
was quite a bit slower than doing
Python:
def g:
    f = lambda ... : ...
    ... = f(...)
any reasons why?
 
Technology news on Phys.org
  • #3
ergospherical said:
any reasons why?
You're redefining the lambda ##f## in every iteration of ##g##. Put the lambda definition of ##f## outside the body of ##g## and see what happens.
 

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
8
Views
817
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
2
Views
790
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
Replies
3
Views
347
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top