1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
|
/*=============================================================================
Copyright (c) 2001-2006 Joel de Guzman
Copyright (c) 2006-2007 Tobias Schwinger
Use modification and distribution are subject to the Boost Software
License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt).
==============================================================================*/
#include <boost/fusion/container/list.hpp>
#include <boost/fusion/container/vector.hpp>
#include <boost/fusion/algorithm/iteration/fold.hpp>
#include <boost/fusion/functional/adapter/unfused_generic.hpp>
#include <boost/fusion/functional/adapter/unfused_rvalue_args.hpp>
#include <boost/fusion/functional/adapter/fused_function_object.hpp>
#include <boost/utility/result_of.hpp>
#include <boost/config.hpp>
#include <boost/timer.hpp>
#include <algorithm>
#include <iostream>
#ifdef _MSC_VER
// inline aggressively
# pragma inline_recursion(on) // turn on inline recursion
# pragma inline_depth(255) // max inline depth
#endif
int const REPEAT_COUNT = 3;
double const duration = 0.125;
namespace
{
struct fused_sum
{
template <typename Seq>
int operator()(Seq const & seq) const
{
int state = 0;
return boost::fusion::fold(seq, state, sum_op());
}
typedef int result_type;
private:
struct sum_op
{
template <typename T>
int operator()(T const & elem, int value) const
{
return value + sizeof(T) * elem;
}
template <typename T>
int operator()(T & elem, int value) const
{
elem += sizeof(T);
return value;
}
typedef int result_type;
};
};
struct unfused_sum
{
inline int operator()() const
{
return 0;
}
template<typename T0>
inline int operator()(T0 const & a0) const
{
return a0;
}
template<typename T0, typename T1>
inline int operator()(T0 const & a0, T1 const & a1) const
{
return a0 + a1;
}
template<typename T0, typename T1, typename T2>
inline int operator()(T0 const & a0, T1 const & a1, T2 a2) const
{
return a0 + a1 + a2;
}
template<typename T0, typename T1, typename T2, typename T3>
inline int operator()(T0 const & a0, T1 const & a1, T2 const & a2, T3 const & a3) const
{
return a0 + a1 + a2 + a3;
}
typedef int result_type;
};
template<typename F>
double call_unfused(F const & func, int & j)
{
boost::timer tim;
int i = 0;
long long iter = 65536;
long long counter, repeats;
double result = (std::numeric_limits<double>::max)();
double runtime = 0;
double run;
do
{
tim.restart();
for(counter = 0; counter < iter; ++counter)
{
i += func();
i += func(0);
i += func(0,1);
i += func(0,1,2);
i += func(0,1,2,3);
}
runtime = tim.elapsed();
iter *= 2;
} while(runtime < duration);
iter /= 2;
for(repeats = 0; repeats < REPEAT_COUNT; ++repeats)
{
tim.restart();
for(counter = 0; counter < iter; ++counter)
{
i = func(); j += i;
i = func(0); j += i;
i = func(0,1); j += i;
i = func(0,1,2); j += i;
i = func(0,1,2,3); j += i;
}
run = tim.elapsed();
result = (std::min)(run, result);
}
return result / iter;
}
template<typename F>
double call_fused_ra(F const & func, int & j)
{
boost::timer tim;
int i = 0;
long long iter = 65536;
long long counter, repeats;
double result = (std::numeric_limits<double>::max)();
double runtime = 0;
double run;
do
{
boost::fusion::vector<> v0;
boost::fusion::vector<int> v1(0);
boost::fusion::vector<int,int> v2(0,1);
boost::fusion::vector<int,int,int> v3(0,1,2);
boost::fusion::vector<int,int,int,int> v4(0,1,2,3);
tim.restart();
for(counter = 0; counter < iter; ++counter)
{
i += func(v0);
i += func(v1);
i += func(v2);
i += func(v3);
i += func(v4);
}
runtime = tim.elapsed();
iter *= 2;
} while(runtime < duration);
iter /= 2;
for(repeats = 0; repeats < REPEAT_COUNT; ++repeats)
{
boost::fusion::vector<> v0;
boost::fusion::vector<int> v1(0);
boost::fusion::vector<int,int> v2(0,1);
boost::fusion::vector<int,int,int> v3(0,1,2);
boost::fusion::vector<int,int,int,int> v4(0,1,2,3);
tim.restart();
for(counter = 0; counter < iter; ++counter)
{
i = func(v0); j += i;
i = func(v1); j += i;
i = func(v2); j += i;
i = func(v3); j += i;
i = func(v4); j += i;
}
run = tim.elapsed();
result = (std::min)(run, result);
}
return result / iter;
}
template<typename F>
double call_fused(F const & func, int & j)
{
boost::timer tim;
int i = 0;
long long iter = 65536;
long long counter, repeats;
double result = (std::numeric_limits<double>::max)();
double runtime = 0;
double run;
do
{
boost::fusion::list<> l0;
boost::fusion::list<int> l1(0);
boost::fusion::list<int,int> l2(0,1);
boost::fusion::list<int,int,int> l3(0,1,2);
boost::fusion::list<int,int,int,int> l4(0,1,2,3);
tim.restart();
for(counter = 0; counter < iter; ++counter)
{
i += func(l0);
i += func(l1);
i += func(l2);
i += func(l3);
i += func(l4);
}
runtime = tim.elapsed();
iter *= 2;
} while(runtime < duration);
iter /= 2;
for(repeats = 0; repeats < REPEAT_COUNT; ++repeats)
{
boost::fusion::list<> l0;
boost::fusion::list<int> l1(0);
boost::fusion::list<int,int> l2(0,1);
boost::fusion::list<int,int,int> l3(0,1,2);
boost::fusion::list<int,int,int,int> l4(0,1,2,3);
tim.restart();
for(counter = 0; counter < iter; ++counter)
{
i = func(l0); j += i;
i = func(l1); j += i;
i = func(l2); j += i;
i = func(l3); j += i;
i = func(l4); j += i;
}
run = tim.elapsed();
result = (std::min)(run, result);
}
return result / iter;
}
}
int main()
{
int total = 0;
int res;
typedef fused_sum F;
typedef unfused_sum U;
std::cout << "Compiler: " << BOOST_COMPILER << std::endl;
std::cout << std::endl << "Unfused adapters:" << std::endl;
{
F f;
std::cout << "F /* a fused function object */ " << call_fused_ra(f,res) << std::endl;
total += res;
}
{
F f;
std::cout << "without random access " << call_fused(f,res) << std::endl;
total += res;
}
{
boost::fusion::unfused_rvalue_args<F> f;
std::cout << "unfused_rvalue_args<F> " << call_unfused(f,res) << std::endl;
total += res;
}
{
boost::fusion::unfused_generic<F> f;
std::cout << "unfused_generic<F> " << call_unfused(f,res) << std::endl;
total += res;
}
std::cout << std::endl << "Fused adapters:" << std::endl;
{
unfused_sum f;
std::cout << "U /* an unfused function object */ " << call_unfused(f,res) << std::endl;
total += res;
}
{
boost::fusion::fused_function_object<U> f;
std::cout << "fused_function_object<U> " << call_fused_ra(f,res) << std::endl;
total += res;
}
{
boost::fusion::fused_function_object<U> f;
std::cout << "without random access " << call_fused(f,res) << std::endl;
total += res;
}
{
boost::fusion::unfused_rvalue_args< boost::fusion::fused_function_object<U> > f;
std::cout << "unfused_rvalue_args<fused_function_object<U> > " << call_unfused(f,res) << std::endl;
total += res;
}
{
boost::fusion::unfused_generic< boost::fusion::fused_function_object<U> > f;
std::cout << "unfused_generic<fused_function_object<U> > " << call_unfused(f,res) << std::endl;
total += res;
}
return total;
}
|