Lambda VS函数

编程入门 行业动态 更新时间:2024-10-27 18:17:35
本文介绍了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函数

本文发布于:2023-11-28 09:21:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641828.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   Lambda

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!