gpt4 book ai didi

c++ - Lambda VS 函数

转载 作者:IT老高 更新时间:2023-10-28 23:03:52 25 4
gpt4 key购买 nike

我刚刚学习完 lambda 表达式,想知道在使用 cout 打印到控制台时,表达式或常规函数是否会执行得更快。

我应该使用

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

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

注意:我仍然是一个新手程序员,所以请随时指出我可能犯的任何错误。我只会学习

谢谢

最佳答案

我认为 lambda 在使用 STL 之类的函数时很优雅,或者您想快速丢弃函数而不命名它们。

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]

两者都有相同的反汇编。

关于c++ - Lambda VS 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25520287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com