I have a code where I have two options, define lambda out of loop to save on lambda creation overhead or define it inside a loop to keep small scope.
Is this choice critical and will make any significant difference?
What are pros and cons of this two choices?
What is best practice?
#include <iostream>
#include <string>
#include <vector>
#include <memory>
int main()
{
std::vector<std::function<void()>> functors;
auto func = [] () { std::cout << "Hello world I am doing some action"; };
//some code here
for(int i = 0; i < 100; ++i)
{
// some code here
functors.push_back(func);
// some code here
}
}
EDITED: simplified example
int main()
{
auto sum = [](const int x, const int y) { return x + y; };
for(int i = 0; i < 100; ++i)
{
std::cout << sum(i, i + 1) << std::endl;
}
}