diff --git a/example/performance/Jamfile.v2 b/example/performance/Jamfile.v2 new file mode 100644 index 00000000..ec8e7b29 --- /dev/null +++ b/example/performance/Jamfile.v2 @@ -0,0 +1,15 @@ +#============================================================================== +# Copyright (c) 2003-2006 Joel de Guzman +# Copyright (c) 2006 Dan Marsden +# +# Use, modification and distribution is 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) +#============================================================================== +project fusion-performance ; + +exe accumulate : accumulate.cpp ; + +exe inner_product : inner_product.cpp ; + +exe inner_product2 : inner_product2.cpp ; diff --git a/example/performance/accumulate.cpp b/example/performance/accumulate.cpp new file mode 100644 index 00000000..ee7248ef --- /dev/null +++ b/example/performance/accumulate.cpp @@ -0,0 +1,353 @@ +/*============================================================================= + Copyright (c) 2001-2006 Joel de Guzman + Copyright (c) 2005-2006 Dan Marsden + + Use, modification and distribution is 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 +#include + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#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 = 10; + +double const duration = 0.5; + +namespace +{ + template + double time_for_std_accumulate(int& j) + { + boost::timer tim; + int i = 0; + long long iter = 65536; + long long counter, repeats; + double result = std::numeric_limits::max(); + double runtime = 0; + double run; + boost::array arr; + std::generate(arr.begin(), arr.end(), rand); + do + { + tim.restart(); + for(counter = 0; counter < iter; ++counter) + { + i = std::accumulate(arr.begin(), arr.end(), 0); + static_cast(i); + } + runtime = tim.elapsed(); + iter *= 2; + } while(runtime < duration); + iter /= 2; + + // repeat test and report least value for consistency: + for(repeats = 0; repeats < REPEAT_COUNT; ++repeats) + { + tim.restart(); + for(counter = 0; counter < iter; ++counter) + { + i = std::accumulate(arr.begin(), arr.end(), 0); + j += i; + } + run = tim.elapsed(); + result = (std::min)(run, result); + } + std::cout << i << std::endl; + return result / iter; + } + + + struct poly_add + { + template + struct result + { + typedef Lhs type; + }; + + template + Lhs operator()(const Lhs& lhs, const Rhs& rhs) const + { + return lhs + rhs; + } + }; + + struct poly_mult + { + template + struct result + { + typedef Lhs type; + }; + + template + Lhs operator()(const Lhs& lhs, const Rhs& rhs) const + { + return lhs * rhs; + } + }; + + template + double time_for_fusion_accumulate(int& j) + { + boost::timer tim; + int i = 0; + long long iter = 65536; + long long counter, repeats; + double result = std::numeric_limits::max(); + double runtime = 0; + double run; + boost::array arr; + std::generate(arr.begin(), arr.end(), rand); + do + { + tim.restart(); + for(counter = 0; counter < iter; ++counter) + { + i = boost::fusion::accumulate(arr, 0, poly_add()); + static_cast(i); + } + runtime = tim.elapsed(); + iter *= 2; + } while(runtime < duration); + iter /= 2; + + std::cout << iter << " iterations" << std::endl; + + // repeat test and report least value for consistency: + for(repeats = 0; repeats < REPEAT_COUNT; ++repeats) + { + tim.restart(); + for(counter = 0; counter < iter; ++counter) + { + i = boost::fusion::accumulate(arr, 0, poly_add()); + j += i; + } + run = tim.elapsed(); + result = (std::min)(run, result); + std::cout << "."; + std::cout.flush(); + } + std::cout << i << std::endl; + return result / iter; + } + +#if 0 + template + double time_for_std_inner_product(int& j) + { + boost::timer tim; + int i = 0; + long long iter = 65536; + long long counter, repeats; + double result = std::numeric_limits::max(); + double runtime = 0; + double run; + boost::array arr1; + boost::array arr2; + std::generate(arr1.begin(), arr1.end(), rand); + std::generate(arr2.begin(), arr2.end(), rand); + do + { + tim.restart(); + for(counter = 0; counter < iter; ++counter) + { + i = std::inner_product(arr1.begin(), arr1.end(), arr2.begin(), 0); + static_cast(i); + } + runtime = tim.elapsed(); + iter *= 2; + } while(runtime < duration); + iter /= 2; + + // repeat test and report least value for consistency: + for(repeats = 0; repeats < REPEAT_COUNT; ++repeats) + { + tim.restart(); + for(counter = 0; counter < iter; ++counter) + { + i = std::inner_product(arr1.begin(), arr1.end(), arr2.begin(), 0); + j += i; + } + run = tim.elapsed(); + result = (std::min)(run, result); + } + std::cout << i << std::endl; + return result / iter; + } + + template + double time_for_fusion_inner_product(int& j) + { + boost::timer tim; + int i = 0; + long long iter = 65536; + long long counter, repeats; + double result = std::numeric_limits::max(); + double runtime = 0; + double run; + boost::array arr1; + boost::array arr2; + std::generate(arr1.begin(), arr1.end(), rand); + std::generate(arr2.begin(), arr2.end(), rand); + do + { + tim.restart(); + for(counter = 0; counter < iter; ++counter) + { + i = boost::fusion::accumulate( + boost::fusion::transform(arr1, arr2, poly_mult()), 0, poly_add()); + static_cast(i); + } + runtime = tim.elapsed(); + iter *= 2; + } while(runtime < duration); + iter /= 2; + + // repeat test and report least value for consistency: + for(repeats = 0; repeats < REPEAT_COUNT; ++repeats) + { + tim.restart(); + for(counter = 0; counter < iter; ++counter) + { + i = boost::fusion::accumulate( + boost::fusion::transform(arr1, arr2, poly_mult()), 0, poly_add()); + j += i; + } + run = tim.elapsed(); + result = (std::min)(run, result); + } + std::cout << i << std::endl; + return result / iter; + } + + struct poly_combine + { + template + struct result + { + typedef Rhs type; + }; + + template + typename result::type + operator()(const Lhs& lhs, const Rhs& rhs) const + { + return rhs + boost::fusion::at_c<0>(lhs) * boost::fusion::at_c<1>(lhs); + } + }; + + template + double time_for_fusion_inner_product2(int& j) + { + boost::timer tim; + int i = 0; + long long iter = 65536; + long long counter, repeats; + double result = std::numeric_limits::max(); + double runtime = 0; + double run; + boost::array arr1; + boost::array arr2; + std::generate(arr1.begin(), arr1.end(), rand); + std::generate(arr2.begin(), arr2.end(), rand); + do + { + tim.restart(); + for(counter = 0; counter < iter; ++counter) + { + i = boost::fusion::accumulate( + boost::fusion::zip(arr1, arr2), 0, poly_combine()); + static_cast(i); + } + runtime = tim.elapsed(); + iter *= 2; + } while(runtime < duration); + iter /= 2; + + std::cout << iter << " iterations" << std::endl; + + // repeat test and report least value for consistency: + for(repeats = 0; repeats < REPEAT_COUNT; ++repeats) + { + tim.restart(); + for(counter = 0; counter < iter; ++counter) + { + i = boost::fusion::accumulate( + boost::fusion::zip(arr1, arr2), 0, poly_combine()); + j += i; + } + run = tim.elapsed(); + result = (std::min)(run, result); + } + std::cout << i << std::endl; + return result / iter; + } +#endif +} + +int main() +{ + int total = 0; + int res; + std::cout << "short accumulate std test " << time_for_std_accumulate<8>(res) << std::endl; + total += res; + std::cout << "short accumulate fusion test " << time_for_fusion_accumulate<8>(res) << std::endl; + total += res; + + std::cout << "medium accumulate std test " << time_for_std_accumulate<64>(res) << std::endl; + total += res; + std::cout << "medium accumulate fusion test " << time_for_fusion_accumulate<64>(res) << std::endl; + total += res; + + std::cout << "long accumulate std test " << time_for_std_accumulate<128>(res) << std::endl; + total += res; + std::cout << "long accumulate fusion test " << time_for_fusion_accumulate<128>(res) << std::endl; + total += res; + +#if 0 + std::cout << "short inner_product std test " << time_for_std_inner_product<8>(res) << std::endl; + total += res; + std::cout << "short inner_product fusion test " << time_for_fusion_inner_product<8>(res) << std::endl; + total += res; + std::cout << "short inner_product fusion 2 test " << time_for_fusion_inner_product2<8>(res) << std::endl; + total += res; + + std::cout << "medium inner_product std test " << time_for_std_inner_product<64>(res) << std::endl; + total += res; + std::cout << "medium inner_product fusion test " << time_for_fusion_inner_product<64>(res) << std::endl; + total += res; + std::cout << "medium inner_product fusion 2 test " << time_for_fusion_inner_product2<64>(res) << std::endl; + total += res; + + + std::cout << "long inner_product std test " << time_for_std_inner_product<128>(res) << std::endl; + total += res; + std::cout << "long inner_product fusion test " << time_for_fusion_inner_product<128>(res) << std::endl; + total += res; + std::cout << "long inner_product fusion 2 test " << time_for_fusion_inner_product2<128>(res) << std::endl; + total += res; +#endif + + return total; +} diff --git a/example/performance/inner_product.cpp b/example/performance/inner_product.cpp new file mode 100644 index 00000000..7ed6283d --- /dev/null +++ b/example/performance/inner_product.cpp @@ -0,0 +1,179 @@ +/*============================================================================= + Copyright (c) 2001-2006 Joel de Guzman + Copyright (c) 2005-2006 Dan Marsden + + Use, modification and distribution is 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 +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#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 = 10; + +double const duration = 0.5; + +namespace +{ + struct poly_add + { + template + struct result + { + typedef Lhs type; + }; + + template + Lhs operator()(const Lhs& lhs, const Rhs& rhs) const + { + return lhs + rhs; + } + }; + + struct poly_mult + { + template + struct result + { + typedef Lhs type; + }; + + template + Lhs operator()(const Lhs& lhs, const Rhs& rhs) const + { + return lhs * rhs; + } + }; + + template + double time_for_std_inner_product(int& j) + { + boost::timer tim; + int i = 0; + long long iter = 65536; + long long counter, repeats; + double result = std::numeric_limits::max(); + double runtime = 0; + double run; + boost::array arr1; + boost::array arr2; + std::generate(arr1.begin(), arr1.end(), rand); + std::generate(arr2.begin(), arr2.end(), rand); + do + { + tim.restart(); + for(counter = 0; counter < iter; ++counter) + { + i = std::inner_product(arr1.begin(), arr1.end(), arr2.begin(), 0); + static_cast(i); + } + runtime = tim.elapsed(); + iter *= 2; + } while(runtime < duration); + iter /= 2; + + // repeat test and report least value for consistency: + for(repeats = 0; repeats < REPEAT_COUNT; ++repeats) + { + tim.restart(); + for(counter = 0; counter < iter; ++counter) + { + i = std::inner_product(arr1.begin(), arr1.end(), arr2.begin(), 0); + j += i; + } + run = tim.elapsed(); + result = (std::min)(run, result); + } + std::cout << i << std::endl; + return result / iter; + } + + template + double time_for_fusion_inner_product(int& j) + { + boost::timer tim; + int i = 0; + long long iter = 65536; + long long counter, repeats; + double result = std::numeric_limits::max(); + double runtime = 0; + double run; + boost::array arr1; + boost::array arr2; + std::generate(arr1.begin(), arr1.end(), rand); + std::generate(arr2.begin(), arr2.end(), rand); + do + { + tim.restart(); + for(counter = 0; counter < iter; ++counter) + { + i = boost::fusion::accumulate( + boost::fusion::transform(arr1, arr2, poly_mult()), 0, poly_add()); + static_cast(i); + } + runtime = tim.elapsed(); + iter *= 2; + } while(runtime < duration); + iter /= 2; + + // repeat test and report least value for consistency: + for(repeats = 0; repeats < REPEAT_COUNT; ++repeats) + { + tim.restart(); + for(counter = 0; counter < iter; ++counter) + { + i = boost::fusion::accumulate( + boost::fusion::transform(arr1, arr2, poly_mult()), 0, poly_add()); + j += i; + } + run = tim.elapsed(); + result = (std::min)(run, result); + } + std::cout << i << std::endl; + return result / iter; + } +} + +int main() +{ + int total = 0; + int res; + + std::cout << "short inner_product std test " << time_for_std_inner_product<8>(res) << std::endl; + total += res; + std::cout << "short inner_product fusion test " << time_for_fusion_inner_product<8>(res) << std::endl; + total += res; + + std::cout << "medium inner_product std test " << time_for_std_inner_product<64>(res) << std::endl; + total += res; + std::cout << "medium inner_product fusion test " << time_for_fusion_inner_product<64>(res) << std::endl; + total += res; + + std::cout << "long inner_product std test " << time_for_std_inner_product<128>(res) << std::endl; + total += res; + std::cout << "long inner_product fusion test " << time_for_fusion_inner_product<128>(res) << std::endl; + total += res; + + return total; +} diff --git a/example/performance/inner_product2.cpp b/example/performance/inner_product2.cpp new file mode 100644 index 00000000..bc104cf6 --- /dev/null +++ b/example/performance/inner_product2.cpp @@ -0,0 +1,199 @@ +/*============================================================================= + Copyright (c) 2001-2006 Joel de Guzman + Copyright (c) 2005-2006 Dan Marsden + + Use, modification and distribution is 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 +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#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 = 10; + +double const duration = 0.5; + +namespace +{ + struct poly_add + { + template + struct result + { + typedef Lhs type; + }; + + template + Lhs operator()(const Lhs& lhs, const Rhs& rhs) const + { + return lhs + rhs; + } + }; + + struct poly_mult + { + template + struct result + { + typedef Lhs type; + }; + + template + Lhs operator()(const Lhs& lhs, const Rhs& rhs) const + { + return lhs * rhs; + } + }; + + template + double time_for_std_inner_product(int& j) + { + boost::timer tim; + int i = 0; + long long iter = 65536; + long long counter, repeats; + double result = std::numeric_limits::max(); + double runtime = 0; + double run; + boost::array arr1; + boost::array arr2; + std::generate(arr1.begin(), arr1.end(), rand); + std::generate(arr2.begin(), arr2.end(), rand); + do + { + tim.restart(); + for(counter = 0; counter < iter; ++counter) + { + i = std::inner_product(arr1.begin(), arr1.end(), arr2.begin(), 0); + static_cast(i); + } + runtime = tim.elapsed(); + iter *= 2; + } while(runtime < duration); + iter /= 2; + + // repeat test and report least value for consistency: + for(repeats = 0; repeats < REPEAT_COUNT; ++repeats) + { + tim.restart(); + for(counter = 0; counter < iter; ++counter) + { + i = std::inner_product(arr1.begin(), arr1.end(), arr2.begin(), 0); + j += i; + } + run = tim.elapsed(); + result = (std::min)(run, result); + } + std::cout << i << std::endl; + return result / iter; + } + + struct poly_combine + { + template + struct result + { + typedef Rhs type; + }; + + template + typename result::type + operator()(const Lhs& lhs, const Rhs& rhs) const + { + return rhs + boost::fusion::at_c<0>(lhs) * boost::fusion::at_c<1>(lhs); + } + }; + + template + double time_for_fusion_inner_product2(int& j) + { + boost::timer tim; + int i = 0; + long long iter = 65536; + long long counter, repeats; + double result = std::numeric_limits::max(); + double runtime = 0; + double run; + boost::array arr1; + boost::array arr2; + std::generate(arr1.begin(), arr1.end(), rand); + std::generate(arr2.begin(), arr2.end(), rand); + do + { + tim.restart(); + for(counter = 0; counter < iter; ++counter) + { + i = boost::fusion::accumulate( + boost::fusion::zip(arr1, arr2), 0, poly_combine()); + static_cast(i); + } + runtime = tim.elapsed(); + iter *= 2; + } while(runtime < duration); + iter /= 2; + + std::cout << iter << " iterations" << std::endl; + + // repeat test and report least value for consistency: + for(repeats = 0; repeats < REPEAT_COUNT; ++repeats) + { + tim.restart(); + for(counter = 0; counter < iter; ++counter) + { + i = boost::fusion::accumulate( + boost::fusion::zip(arr1, arr2), 0, poly_combine()); + j += i; + } + run = tim.elapsed(); + result = (std::min)(run, result); + } + std::cout << i << std::endl; + return result / iter; + } +} + +int main() +{ + int total = 0; + int res; + + std::cout << "short inner_product std test " << time_for_std_inner_product<8>(res) << std::endl; + total += res; + std::cout << "short inner_product fusion 2 test " << time_for_fusion_inner_product2<8>(res) << std::endl; + total += res; + + std::cout << "medium inner_product std test " << time_for_std_inner_product<64>(res) << std::endl; + total += res; + std::cout << "medium inner_product fusion 2 test " << time_for_fusion_inner_product2<64>(res) << std::endl; + total += res; + +#if 0 // Leads to ICE with MSVC 8.0 + std::cout << "long inner_product std test " << time_for_std_inner_product<128>(res) << std::endl; + total += res; + std::cout << "long inner_product fusion 2 test " << time_for_fusion_inner_product2<128>(res) << std::endl; + total += res; +#endif + + return total; +}