Python Speed of function vs lambda calls in python

Click For Summary
The discussion centers on the performance difference between using a standard function definition and a lambda function within another function in Python. It highlights that defining a lambda function inside a function leads to the lambda being redefined with each iteration, which can slow down execution. In contrast, defining the lambda outside the function allows it to be reused, improving performance. The conversation references a Stack Overflow post indicating that the execution time difference is generally negligible, but emphasizes that the context of how functions are defined and called can impact speed.
ergospherical
Science Advisor
Homework Helper
Education Advisor
Insights Author
Messages
1,100
Reaction score
1,387
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
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.
 
Anthropic announced that an inflection point has been reached where the LLM tools are good enough to help or hinder cybersecurity folks. In the most recent case in September 2025, state hackers used Claude in Agentic mode to break into 30+ high-profile companies, of which 17 or so were actually breached before Anthropic shut it down. They mentioned that Clause hallucinated and told the hackers it was more successful than it was...

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 18 ·
Replies
18
Views
1K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 43 ·
2
Replies
43
Views
3K