c++ std::function 比 c function 慢

本文通过测试用例分析了C++中std::function与C函数、functor、类方法的性能差异。在不同优化级别下,std::bind生成的function在未优化时表现不佳,即使在O3优化后仍明显慢于C函数。类方法的表现接近C函数,而结构体嵌套类指针的方式相对稳定。测试结果显示,类型擦除在std::function中的应用带来了性能开销。

项目中大量使用functional来替代virtual函数,那么functor函数相对于c function带来的额外成本是多少呢?今天构造一个测试用例来测试下。g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-28)

测试如下几种情况:

1.c function

int _func(int a, int b) {
  return a + b;
}

2.重载operator()的struct

struct _struct {
  int operator()(int a, int b) {
     return a + b;
  }
};

3.类方法

class _Class{
public:
   int op(int a, int b) {return a + b;} 
};

4.结构体嵌套类指针

struct _struct2 {
  _Class *p; 
  int operator()(int a, int b) {
     return p->op(a, b); 
  }
};

对如上几种方式分别调用100000次。分别在O0 O1 O3 测试结果如下

 time(O0)time(O1)time(O3)
纯函数3227846
结构体3457735
3637233
类bind17925658433
functional2506548288
结构体嵌套类7139136

结论

我们可以得出一个结论,g++不提供优化参数时, std::bind返回的function性能堪忧,到O3也和纯c相差甚远。同时,类方法的结果也印证了c++内存模型所讲的类方法(非virtual)只是改了名字的c方法。

c++中的functional为什么性能和c function相差这么远?这个结果与常规的认知(结构体内包含类指针)不一致,采用类型擦除手段会带来这么大的负担? 看结果通过结构体保存类指针的方式比较稳定。

 

测试代码如下

#include <iostream>
#include <functional>
#include <sys/time.h>


int _func(int a, int b) {
  return a + b;
}

struct _struct {
  int operator()(int a, int b) {
     return a + b;
  }
};

class _Class{
public:
   int op(int a, int b) {return a + b;}
};

struct _struct2 {
  _Class *p;
  int operator()(int a, int b) {
     return p->op(a, b);
  }
};


// 纯函数
void test_func() {
  printf("测试纯函数\n");
  int i = 0;
  int s = 0;
  while(i++ < 100000) {
    s+=_func(3, 4);
  }
}

// 测试结构体实现的functor
void test_struct() {
  printf("测试结构体\n");
  int i = 0;
  int s = 0;
  struct _struct f;
  while(i++ < 100000) {
    s+=f(3, 4);
  }
}

// 通过类调用的结构体
void test_struct2() {
  printf("测试结构体嵌套类\n");
  int i = 0;
  int s = 0;
  struct _struct2 f;
  _Class t;
  f.p = &t;
  while(i++ < 100000) {
    s+=f(3, 4);
  }
}


// 测试类
void test_class() {
  printf("测试结类\n");
  int i = 0;
  int s = 0;
  _Class t;
  while(i++ < 100000) {
    s+=t.op(3, 4);
  }
}


// 测试bind class
void test_class_functor() {
  printf("测试类bind\n");
  int i = 0;
  int s = 0;
  _Class t;
  std::function<int(int, int)> functor = std::bind(&_Class::op, &t, std::placeholders::_1, std::placeholders::_2);
  while(i++ < 100000) {
    s+=functor(3, 4);
  }
}


// 测试function
void test_functor() {
  printf("测试function\n");
  int i = 0;
  int s = 0;
  std::function<int(int, int)> functor2 = [](int a, int b) -> int {return a + b;};
  while(i++ < 100000) {
    s+=functor2(3, 4);
  }
}

int main(int argc, const char **argv)
{
  struct timeval tv;
  gettimeofday(&tv, NULL);
  if (argv[1][0] == 'a') test_func(); 
  if (argv[1][0] == 'b') test_struct(); 
  if (argv[1][0] == 'c') test_struct2(); 
  if (argv[1][0] == 'd') test_class(); 
  if (argv[1][0] == 'e') test_class_functor(); 
  if (argv[1][0] == 'f') test_functor(); 
  struct timeval tv2;
  gettimeofday(&tv2, NULL);
  int d = (tv2.tv_sec * 1000000 + tv2.tv_usec) - (tv.tv_sec * 1000000 + tv.tv_usec);
  printf("%d \n", d);
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值