+ 1
Importance of Lambda function
Hi Below is same code for reference: https://code.sololearn.com/c9MtZMEX3xLy/?ref=app As we can see that earlier approach is better in terms of code readability (might be as we are more used to it) compare to lamdba function approach. Is there any impact of performance with usage of Lambda over earlier approach ? What is importance of introduction to lamdba function
6 odpowiedzi
+ 2
Ketan Lalcheta
Actually, the whole point of using lambda is that they don’t have overhead compared to function calls, but performance overhead these days is negligible because modern architectures are really good at predicting and calling with about 1-2 cycles only.
Unless your code runs in a loop and is executed a million times, you should focus on producing clean and understandable code instead.
+ 1
I would say the big difference is the length of the code. Functions are typically longer than lambdas, especially if you split declaration and implementation (.hpp and .cpp file). Additionally you don't need to name them (big advantage :)
Therefore a short one-time-use function could/should be replaced with a lambda.
Write longer or often used code snippets as functions.
+ 1
I can think of only one, maybe two instances where lambda is .. nice to have, but never necessary.
1. when you want to create a function that captures local variables without declaring it outside.
2. comparators
To answer your question: no, there isn’t any impact in performance. Try to keep things simple. If you can get things done just fine with your old ways, stick to it.
0
Bobby Fischer yeah , first point is good.... We can pass all through capture list ....
0
Michi , Bobby Fischer and all,
What is running in my mind is related to inline function... Does writting lambda help us to reduce function call overhead?
- 1
Michi I would like to have different opinions... What is the advantage of not naming the function? If we have name, we can call it as many time as we want... That is the main purpose of functions to have a piece of code being called more time