C++ – Lambda VS Function

c++functionlambda

I just finished learning about lambda expressions and was wondering whether an expression or a regular function would execute faster when printing to a console using cout.

Should I use

// Lambda expression
auto helloWorld = []()
{
    cout << "Hello World" << endl;
};

or

// Normal function
void helloWorld()
{
    cout << "Hello World" << endl;
}

Note: I am still a novice programmer, so do feel free to point out any errors I may have made. I can only learn

Thanks

Best Solution

I think lambda is elegant when using stl functions like , or you want quick throw away functions without naming them .

sort(v.begin(),
     v.end(),
     [](int a, int b){ return a > b; }
);

but it's not faster from the function .

Disassembly of both .

    helloWorld1();
008112A0  mov         ecx,dword ptr ds:[813054h]  
008112A6  push        8119A0h  
008112AB  call        std::operator<<<std::char_traits<char> > (0811780h)  
008112B0  mov         ecx,eax  
008112B2  call        dword ptr ds:[813038h]  
    helloWorld2();
008112B8  mov         ecx,dword ptr ds:[813054h]  
008112BE  push        8119A0h  
008112C3  call        std::operator<<<std::char_traits<char> > (0811780h)  
008112C8  mov         ecx,eax  
008112CA  call        dword ptr ds:[813038h] 

both have the same disassembly.