In JavaScript, Mozilla recommends that functions should not be created inside other functions if closures are not needed, as it will negatively affect script performance. In JavaScript, this same concern is applicable when creating functions inside of loops. Does the same concern apply to C++ lambdas?
For example, is there a performance difference between these two functions:
int f1(vector<int> v) {
for_each(v.begin(), v.end(), [](int i) { cout << i << endl; });
}
auto print_int = [](int i) { cout << i << endl; };
int f2(vector<int> v) {
for_each(v.begin(), v.end(), print_int);
}
I would assume yes, these concerns apply to C++ and that f2
will perform better than f1
; however, I haven't been able to find a definite answer.