64,386
社区成员
发帖
与我相关
我的任务
分享
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <Windows.h>
#include <stdio.h>
#define MAX_LOOP 10000000
#define CONST_NUM 3
typedef boost::function<void()> task;
class VBase
{
public:
virtual void say();
};
class VChild:public VBase
{
public:
VChild(int);
void say();
private:
int count;
};
class BChild
{
public:
BChild(int);
void say();
private:
int count;
};
void VBase::say()
{
}
VChild::VChild(int count)
{
this->count = count;
}
void VChild::say()
{
this->count = this->count*CONST_NUM;
this->count = this->count/CONST_NUM;
this->count = this->count+CONST_NUM;
this->count = this->count-CONST_NUM;
}
BChild::BChild(int c)
{
this->count = c;
}
void BChild::say()
{
this->count = this->count*CONST_NUM;
this->count = this->count/CONST_NUM;
this->count = this->count+CONST_NUM;
this->count = this->count-CONST_NUM;
}
void runVFunction(VBase *pObject)
{
int start = ::GetTickCount();
for(int i=0 ; i < MAX_LOOP ;++i)
{
pObject->say();
}
printf("virtual function :%ims\n",(::GetTickCount()-start));
}
void runBind(task func)
{
int start = ::GetTickCount();
for(int i=0 ; i < MAX_LOOP ;++i)
{
func();
}
printf("boost::bind :%ims\n",(::GetTickCount()-start));
}
int main()
{
VChild *vObj = new VChild(10);
BChild *bObj = new BChild(10);
runVFunction(vObj);
runBind(boost::bind(&BChild::say,bObj));
system("pause");
return 0;
}
//
// bind_vir_test.cxx
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// file create 2009-04-03
// last update 2009-04-03
//
// Bugs Report <zhuxueling@live.cn>
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include<traxex/Function.hpp>
#include<traxex/ToolKit.hpp>
#include<iostream>
using namespace Traxex;
long long int i;
struct B{
virtual void do_someting() = 0;
};
struct T:public B{
void do_someting(){
i *=2;
i /=2;
i +=2;
}
};
int test0(){
double time_start = time_now();
T t;
for( int i = 0; i < 100000000; ++i){
t.do_someting();
}
double time_end = time_now();
double time_used = time_end - time_start;
std::cout << "time_use0: " << time_used << std::endl;
}
int test1(){
double time_start = time_now();
T t;
function<void()> func = bind( &T::do_someting, &t);
for( int i = 0; i < 100000000; ++i){
func();
}
double time_end = time_now();
double time_used = time_end - time_start;
std::cout << "time_use1: " << time_used << std::endl;
}
int test2(){
double time_start = time_now();
T t;
for( int i = 0; i < 100000000; ++i){
function<void()> func = bind( &T::do_someting, &t);
func();
}
double time_end = time_now();
double time_used = time_end - time_start;
std::cout << "time_use2: " << time_used << std::endl;
}
int test3(){
double time_start = time_now();
B *t = new T();
for( int i = 0; i < 100000000; ++i){
t->do_someting();
}
double time_end = time_now();
double time_used = time_end - time_start;
std::cout << "time_use3: " << time_used << std::endl;
}
int main(){
test0();
test1();
test2();
test3();
}
class BaseProtocol
{
public:
virtual void onData(void *pData,sizt_t len);
};
class ServerProtocol:baseProtocol
{
public:
virtual void onData(void *pData,sizt_t len)
{
//有数据来了~
}
virtual void onAccept(socket *pClient)
{
//有连接来了~
}
};
//网络处理层,形参是基协议类指针,OOP的多态
void core(BaseProtocol *p)
{
//这里socket处理
while(1)
{
int n = epoll_wait(.....);
//有连接了就调 p->onAccept
//有数据了就调 p->onData
}
}
int main()
{
//这里我要处理ServerProtocol这个协议
ServerProtocol *pService = new ServerProtocol();
core(pService);//工作 传的是ServerProtocol指针
}
//这个是boost::function对象,类似C#的委托.
typedef boost::function<void (socket *)> acceptHandle;
typedef boost::function<void (void *,size_t)>dataHandle;
class BaseProtocol
{
public:
//普通成员函数即可
void onData(void *pData,sizt_t len);
};
class ServerProtocol
{
public:
void onData(void *pData,sizt_t len)
{
//有数据
}
void onAccept(socket *pClient)
{
// 有连接
}
};
//这里传boost::function ,核心思想是,只关心能调用的东西的"样子"
void core(acceptHandle ah,dataHandle dh)
{
//这里处理网络
while(1)
{
int n = epoll_wait(.....);
//有连接了就 直接 ah(....);
//有数据了就调 dh(....);
}
}
int main()
{
//这里我要处理ServerProtocol这个协议
ServerProtocol *pService = new ServerProtocol();
core(boost:bind(&ServerProtocol::onAccept,pService),boost::bind(&ServerProtocol::onData,pService,_1,_2));//工作 boost::bind把协议对象,函数联系起来,当然core是不关心这些的,他只关心"样子"
}
class BaseProtocol
{
public:
virtual void onData(void *pData,sizt_t len);
virtual void onAccept(socket *pClient);
};
class ServerProtocol:baseProtocol
{
public:
virtual void onData(void *pData,sizt_t len){}
virtual void onAccept(socket *pClient){}
};
//
void core(BaseProtocol *p)
{
//这里socket处理
while(1)
{
int n = epoll_wait(.....);
//有连接了就调 p->onAccept
//有数据了就调 p->onData
}
}
int main()
{
//这里我要处理ServerProtocol这个协议
ServerProtocol *pService = new ServerProtocol();
core(pService);//工作
}
typedef boost::function<void (socket *)> acceptHandle;
typedef boost::function<void (void *,size_t)>dataHandle;
class BaseProtocol
{
public:
void onData(void *pData,sizt_t len);
void onAccept(socket *pClient);
};
class ServerProtocol
{
public:
void onData(void *pData,sizt_t len){}
void onAccept(socket *pClient){}
};
void core(acceptHandle ah,dataHandle dh)
{
//这里处理网络
while(1)
{
int n = epoll_wait(.....);
//有连接了就 直接 ah(....);
//有数据了就调 dh(....);
}
}
int main()
{
//这里我要处理ServerProtocol这个协议
ServerProtocol *pService = new ServerProtocol();
core(boost:bind(&ServerProtocol::onAccept,pService),boost::bind(&ServerProtocol::onData,pService,_1,_2));//工作
}