64,646
社区成员
发帖
与我相关
我的任务
分享
class handler_allocator
{
public:
//分配与回收内存相关的操作
private:
bool in_use_;
char storage_[1024];
};
template <typename Handler>
class custom_alloc_handler
{
public:
//管理内存;进行回调相关
private:
handler_allocator& allocator_;
Handler handler_;
};
template <typename Handler>
inline custom_alloc_handler<Handler> make_custom_alloc_handler(
handler_allocator& a, Handler h)
{
return custom_alloc_handler<Handler>(a, h);
}
int Run(int run_times)
{
handler_allocator* ha = new handler_allocator[run_times];
boost::posix_time::ptime t1 = boost::posix_time::microsec_clock::local_time();
for (int i = 0; i < run_times; ++i)
{
sck_.async_read_some(boost::asio::buffer(buffer_, sizeof(buffer_)), strand_.wrap(
make_custom_alloc_handler(ha[i], boost::bind(&TestAllocator::RunHandler, this,
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred))));
/*sck_.async_read_some(boost::asio::buffer(buffer_, sizeof(buffer_)), boost::bind(&TestAllocator::RunHandler,
this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));*/
}
boost::posix_time::ptime t2 = boost::posix_time::microsec_clock::local_time();
return (t2 - t1).total_microseconds();
}
int main(int argc, char* argv[])
{
int run_times = 1000000;
int ret = 0;
for (int i = 0; i < 20; ++i)
{
ret += Run(run_times);
}
cout << "basic: " << (ret / 20) << endl;
return 0;
}