#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/scoped_ptr.hpp>
#include <Windows.h>
#include <stdio.h>
#define MAX_LOOP 1000*1000*8
typedef boost::function1<void, int> Funtor;
struct TBase{
virtual int DoSomething(int i) = 0;
};
struct TChiled : public TBase
{
int DoSomething(int i){
Count(i);
return 0;
}
int DoSomethingNotVitrual(int i){
Count(i);
return 0;
}
int Count(int i){
i++;
i *= 2;
i /= 4;
return i;
}
};
void TestMemberFunction(){
TChiled* pChiled = new TChiled();
DWORD nBeginTime = GetTickCount();
for (int i=0; i<MAX_LOOP; i++)
{
pChiled->DoSomethingNotVitrual(i);
}
printf(
boost bind性能开销分析
最新推荐文章于 2021-07-22 20:28:38 发布
通过对不同编译选项的测试,发现成员函数调用和虚函数调用的性能相近,优于封装到functor中。使用Boost Bind创建临时functor会增加额外开销,尤其是在涉及多次拷贝构造时。scope_ptr的开销较小,而堆上分配对象的开销显著。当参数复杂或数量多时,bind的开销会增加,可能涉及动态内存分配。因此,建议在性能关键路径(hot path)避免过多使用智能指针、new操作、bind和虚指针,优先考虑直接调用对象成员方法。
摘要由CSDN通过智能技术生成