Speed of function vs lambda calls in python

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 4K views
Science Advisor
Homework Helper
Education Advisor
Insights Author
Messages
1,098
Reaction score
1,385
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?
 
Physics news on Phys.org
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.