【问题标题】:Why is lambda slower then std::function?为什么 lambda 比 std::function 慢?
【发布时间】:2018-11-23 12:44:45
【问题描述】:

我很快写了一个“简单”但希望不会太愚蠢的基准测试 始终尝试不将 lambda 分配给 std::function 的方法(因为它较慢?-> 至少一个虚函数(不确定))是有意义的。

#include <iostream>
#include <functional>
#include <chrono>

typedef std::chrono::high_resolution_clock::time_point TimeVar;
#define duration(a) std::chrono::duration_cast<std::chrono::nanoseconds>(a).count()
#define timeNow() std::chrono::high_resolution_clock::now()

template<typename T, typename F>
auto time(T t, F &&rF)
{
    std::cout << t  << "\t";
    TimeVar t1=timeNow();

    int a = rand()%10;
    for(int i=0;i<10000000;i++)
    {
        a = rF(a);
    }
    auto count = duration(timeNow()-t1);
    std::cout << "a: " << a << " time: " << count << " ns " << std::endl;
    return count;
}

template<typename T>
auto timeN(T t)
{
    std::cout << t << "\t";
    TimeVar t1=timeNow();

    int a = rand()%10;
    for(int i=0;i<10000000;i++)
    {
        a = a + rand()%10;
    }
    auto count = duration(timeNow()-t1);
    std::cout << "a: " << a << " time: " << count << " ns " << std::endl;
    return count;
}


int main()
{
   auto c1 = time("lambda: ", [](int a) { return a + rand()%10; });
   auto c2 = time("std::function: ", std::function<int(int)>{[](int a) { return a + rand()%10; }}); 
   auto c3 = timeN("baseline: "); 

   std::cout << std::endl;
   std::cout << "lambda: \t" << (float)c1/c3  << " x slower then baseline" << std::endl;
   std::cout << "std::function: \t" << (float)c2/c3  << " x slower then baseline" << std::endl;

   std::cout << "std::function: \t" << (float)c2/c1  << " x slower then lambda" << std::endl;
}

Live

输出:

lambda:     a: 45011713 time: 182743890 ns 
std::function:  a: 45000320 time: 161290160 ns 
baseline:   a: 45004251 time: 134701347 ns 

lambda:     1.35666 x slower then baseline
std::function:  1.19739 x slower then baseline
std::function:  0.882602 x slower then lambda

这让我很困惑,因为 lambda 比 std::function 慢。为什么?我的意思是 lambda 的类型是编译器生成的,我假设它可以优化更多而不是不透明的 std::function 类型。 (如果你用 clang 编译你会得到相反的结果)。 问题也出现了,如果 std::function 是不透明类型,为什么它会这么快......?对 lambdas 的赋值是特殊的并优化了吗?

【问题讨论】:

  • 你编译时是否开启了优化?
  • 恕我直言,宏并没有真正帮助提高可读性,您没有使用funcTime,是吗?
  • 哦,天哪,忘记了 -O3 选项...清理了代码
  • 对不起,如果我让你破坏了你的代码,现在timeNow 的定义丢失了

标签: c++ lambda c++17 std-function


【解决方案1】:

如果启用优化没有太大区别:

~$ g++ -O3 --std=c++14 a.cpp
~$ ./a.out
lambda:     a: 45002817 time: 63951040 ns
std::function:  a: 45002682 time: 64764776 ns
baseline:   a: 44990972 time: 62371825 ns

lambda:     1.02532 x slower then baseline
std::function:  1.03837 x slower then baseline
std::function:  1.01272 x slower then lambda
~$ ./a.out
lambda:     a: 45002817 time: 63315194 ns
std::function:  a: 45002682 time: 63703902 ns
baseline:   a: 44990972 time: 64156841 ns

lambda:     0.986881 x slower then baseline
std::function:  0.99294 x slower then baseline
std::function:  1.00614 x slower then lambda
~$ ./a.out
lambda:     a: 45002817 time: 64336198 ns
std::function:  a: 45002682 time: 64334809 ns
baseline:   a: 44990972 time: 62341043 ns

lambda:     1.032 x slower then baseline
std::function:  1.03198 x slower then baseline
std::function:  0.999978 x slower then lambda
~$ ./a.out
lambda:     a: 45002817 time: 64270878 ns
std::function:  a: 45002682 time: 63267123 ns
baseline:   a: 44990972 time: 62374261 ns

lambda:     1.03041 x slower then baseline
std::function:  1.01431 x slower then baseline
std::function:  0.984382 x slower then lambda
~$ ./a.out
lambda:     a: 45002817 time: 63049074 ns
std::function:  a: 45002682 time: 65208456 ns
baseline:   a: 44990972 time: 62404926 ns

lambda:     1.01032 x slower then baseline
std::function:  1.04492 x slower then baseline
std::function:  1.03425 x slower then lambda
~$ ./a.out
lambda:     a: 45002817 time: 67177288 ns
std::function:  a: 45002682 time: 65373651 ns
baseline:   a: 44990972 time: 63936167 ns

lambda:     1.05069 x slower then baseline
std::function:  1.02248 x slower then baseline
std::function:  0.973151 x slower then lambda
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-22
  • 2011-04-07
  • 2017-12-16
  • 2015-04-18
  • 1970-01-01
  • 1970-01-01
  • 2019-03-01
相关资源
最近更新 更多