C++ Pirate: Lambda vs Bind (三)

2014-11-23 22:54:04 · 作者: · 浏览: 24
这在对象内部是非常复杂的。boost:bind那么老方法boost::bind又是怎样的呢?为了简单起见,我们在上面的测试用例中直接用boost来替代std。Accumulate (boost bind) 3223174
Accumulate (boost bound lambda) 4255098令人感到奇怪的是boost::bind要比std::bind要快25%左右,boost::bind的调用堆栈与std::bind的看起来很相像:#0 test_accumulate_bind_function (x=@0x7fffffffe600, i=0) at lambda_vs_bind.cpp:114
#1 0x00000000004018a3 in operator() (a0=0, this=) at /usr/local/include/boost/function/function_template.hpp:1013
#2 do_test_loop > (upper_limit=, func=) at lambda_vs_bind.cpp:101
#3 test_accumulate_boost_bind () at lambda_vs_bind.cpp:144
#4 0x0000000000401f44 in run_test (name=, func=0x401800 ) at lambda_vs_bind.cpp:92
#5 0x000000000040207e in main () at lambda_vs_bind.cpp:161(我大概可以写一整篇的文章来描述问什么boost::bind要比std::bind快了... ...)[cpp] view plaincopyprint functional template inline typename _Bind_helper<_Functor, _ArgTypes...>::type bind(_Functor&& __f, _ArgTypes&&... __args) { typedef _Bind_helper<_Functor, _ArgTypes...> __helper_type; typedef typename __helper_type::__maybe_type __maybe_type; typedef typename __helper_type::type __result_type; return __result_type(__maybe_type::__do_wrap(std::forward<_Functor>(__f)), std::forward<_ArgTypes>(__args)...); } boost/bind/bind.hpp (with the macros expanded) template _bi::bind_t<_bi::unspecified, F, typename _bi::list_av_2::type> bind(F f, A1 a1, A2 a2) { typedef typename _bi::list_av_2::type list_type; return _bi::bind_t<_bi::unspecified, F, list_type> (f, list_type(a1, a2)); } functional
template
inline
typename _Bind_helper<_Functor, _ArgTypes...>::type
bind(_Functor&& __f, _ArgTypes&&... __args)
{
typedef _Bind_helper<_Functor, _ArgTypes...> __helper_type;
typedef typename __helper_type::__maybe_type __maybe_type;
typedef typename __helper_type::type __result_type;
return __result_type(__maybe_type::__do_wrap(std::forward<_Functor>(__f)),
std::forward<_ArgTypes>(__args)...);
}
boost/bind/bind.hpp (with the macros expanded)
template
_bi::bind_t<_bi::unspecified, F, typename _bi::list_av_2::type>
bind(F f, A1 a1, A2 a2)
{
typedef typename _bi::list_av_2::type list_type;
return _bi::bind_t<_bi::unspecified, F, list_type> (f, list_type(a1, a2));
}更多信息--------------------------------------------------------------------------------1. 源代码你可以从这里获取到该程序的源代码http://www.gockelhut.com/c++/files/lambda_vs_bind.cpp。它在g++ 4.6.2的编译器上通过了编译并且能够运行,在支持c++11更好的编译器上编译将会更好。我的Boost库的版本是1.47,较早的版本和更新的版本的库都将工作得很好,因为boost::bind语法在一段时间内不会有太大更新(将来不一定)。如果你希望编译和运行都不用boost,那么将USE_BOOST的值改为0即可。2. volatile_writevolatile_write函数是一个由我编写的强制的让 系统在内存中写数据的简单函数,这样就能防止优化器去优化那些在函数run_test中没有做任何事情的代码。[cpp] view plaincopyprint template void volatile_write(const T& x) { volatile T* p = new T; *p = x; delete p; } template
void volatile_write(const T& x)
{
volatile T* p = new T;
*p = x;
delete p;
}--------------------------------------------------------------------------------原文地址:http://www.gockelhut.com/c++/articles/lambda_vs_bind
lambda_vs_bind.cpp[cpp] view plaincopyprint /** * Copyright 2011 Travis Gockel * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, *