Lambda VS函数 [英] Lambda VS Function

查看:93
本文介绍了Lambda VS函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚学完lambda表达式,并想知道使用 cout 打印到控制台时,表达式还是常规函数执行速度会更快。

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;
};

// 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

推荐答案

当使用 stl 函数时,或者您想快速抛弃函数而不命名它们。

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; }
);

但函数不会更快。

反汇编这两者。

    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] 

都具有相同的反汇编。

这篇关于Lambda VS函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆