Python Speed of function vs lambda calls in python

AI Thread 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,097
Reaction score
1,384
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
2
Views
1K
Replies
15
Views
2K
Replies
18
Views
1K
Replies
8
Views
1K
Replies
7
Views
5K
Replies
9
Views
2K
Replies
14
Views
4K
Back
Top