项目中大量使用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) | |
|---|---|---|---|
| 纯函数 | 322 | 78 | 46 |
| 结构体 | 345 | 77 | 35 |
| 类 | 363 | 72 | 33 |
| 类bind | 17925 | 658 | 433 |
| functional | 2506 | 548 | 288 |
| 结构体嵌套类 | 713 | 91 | 36 |
结论
我们可以得出一个结论,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);
}
本文通过测试用例分析了C++中std::function与C函数、functor、类方法的性能差异。在不同优化级别下,std::bind生成的function在未优化时表现不佳,即使在O3优化后仍明显慢于C函数。类方法的表现接近C函数,而结构体嵌套类指针的方式相对稳定。测试结果显示,类型擦除在std::function中的应用带来了性能开销。
2314

被折叠的 条评论
为什么被折叠?



