+ 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

30th May 2020, 7:28 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
6 Réponses
+ 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.
31st May 2020, 7:33 AM
Bobby Fischer
Bobby Fischer - avatar
+ 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.
30th May 2020, 8:11 PM
Michi
Michi - avatar
+ 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.
30th May 2020, 8:17 PM
Bobby Fischer
Bobby Fischer - avatar
0
Bobby Fischer yeah , first point is good.... We can pass all through capture list ....
31st May 2020, 7:19 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
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?
31st May 2020, 7:22 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
- 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
31st May 2020, 7:21 AM
Ketan Lalcheta
Ketan Lalcheta - avatar