Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r70954 - sandbox/local/libs/local/example
From: lorcaminiti_at_[hidden]
Date: 2011-04-03 13:55:52


Author: lcaminiti
Date: 2011-04-03 13:55:51 EDT (Sun, 03 Apr 2011)
New Revision: 70954
URL: http://svn.boost.org/trac/boost/changeset/70954

Log:
Added benchmark and template-parameter-trick examples.
Added:
   sandbox/local/libs/local/example/add_using_global_functor.cpp (contents, props changed)
   sandbox/local/libs/local/example/benchmark_boost_lambda.cpp (contents, props changed)
   sandbox/local/libs/local/example/benchmark_boost_local.cpp (contents, props changed)
   sandbox/local/libs/local/example/benchmark_boost_phoenix.cpp (contents, props changed)
   sandbox/local/libs/local/example/benchmark_global_functor.cpp (contents, props changed)
   sandbox/local/libs/local/example/benchmark_local_functor.cpp (contents, props changed)
   sandbox/local/libs/local/example/tparam_trick.cpp (contents, props changed)

Added: sandbox/local/libs/local/example/add_using_global_functor.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/add_using_global_functor.cpp 2011-04-03 13:55:51 EDT (Sun, 03 Apr 2011)
@@ -0,0 +1,43 @@
+
+// Copyright (C) 2009-2011 Lorenzo Caminiti
+// Use, modification, and distribution is subject to the Boost Software
+// License, Version 1.0 (see accompanying file LICENSE_1_0.txt or a
+// copy at http://www.boost.org/LICENSE_1_0.txt).
+
+//[ add_using_global_functor_cpp
+#include <iostream>
+#include <vector>
+#include <algorithm>
+
+// Unfortunately, cannot be defined locally (so this is not an alternative
+// implementation of local functions).
+struct global_add { // Unfortunately, boilerplate code to program the class.
+ global_add(double& _sum, int _factor): sum(_sum), factor(_factor) {}
+
+ void operator()(double num) {
+ // Body uses C++ statement syntax.
+ sum += factor * num;
+ std::cout << "Summed: " << sum << std::endl;
+ }
+
+private:
+ // Unfortunately, cannot bind so repeat variable types.
+ double& sum; // Access `sum` by reference.
+ const int factor; // Make `factor` constant.
+};
+
+int main() {
+ double sum = 0.0;
+ int factor = 10;
+
+ std::vector<double> v(3);
+ v[0] = 1.0; v[1] = 2.0; v[2] = 3.0;
+
+ global_add add(sum, factor);
+ std::for_each(v.begin(), v.end(), add); // Passed as template parameter.
+
+ std::cout << sum << std::endl;
+ return 0;
+}
+//]
+

Added: sandbox/local/libs/local/example/benchmark_boost_lambda.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/benchmark_boost_lambda.cpp 2011-04-03 13:55:51 EDT (Sun, 03 Apr 2011)
@@ -0,0 +1,28 @@
+
+#include <boost/lambda/lambda.hpp>
+#include <iostream>
+#include <vector>
+#include <algorithm>
+
+#define N 10000
+
+int main() {
+ using boost::lambda::_1;
+ using boost::lambda::var;
+
+ double sum = 0.0;
+ int factor = 10;
+
+ std::vector<double> v(N * 100);
+ std::fill(v.begin(), v.end(), 10);
+
+ for (size_t n = 0; n < N; ++n) {
+ std::for_each(v.begin(), v.end(), (
+ sum += factor * _1
+ ));
+ }
+
+ std::cout << sum << std::endl;
+ return 0;
+}
+

Added: sandbox/local/libs/local/example/benchmark_boost_local.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/benchmark_boost_local.cpp 2011-04-03 13:55:51 EDT (Sun, 03 Apr 2011)
@@ -0,0 +1,27 @@
+
+#include <boost/local/function.hpp>
+#include <vector>
+#include <algorithm>
+#include <iostream>
+
+#define N 10000
+
+int main() {
+ double sum = 0.0;
+ int factor = 10;
+
+ void BOOST_LOCAL_FUNCTION_PARAMS( (const double& num)
+ (bind& sum) (const bind& factor) ) {
+ sum += factor * num;
+ } BOOST_LOCAL_FUNCTION_NAME(add)
+
+ std::vector<double> v(N * 100);
+ std::fill(v.begin(), v.end(), 10);
+ for (size_t n = 0; n < N; ++n) {
+ std::for_each(v.begin(), v.end(), add);
+ }
+
+ std::cout << sum << std::endl;
+ return 0;
+}
+

Added: sandbox/local/libs/local/example/benchmark_boost_phoenix.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/benchmark_boost_phoenix.cpp 2011-04-03 13:55:51 EDT (Sun, 03 Apr 2011)
@@ -0,0 +1,32 @@
+
+#include <boost/spirit/include/phoenix.hpp>
+#include <iostream>
+#include <vector>
+#include <algorithm>
+
+#define N 10000
+
+int main() {
+ using boost::phoenix::let;
+ using boost::phoenix::local_names::_f;
+ using boost::phoenix::cref;
+ using boost::phoenix::ref;
+ using boost::phoenix::arg_names::_1;
+ using boost::phoenix::val;
+
+ double sum = 0.0;
+ int factor = 10;
+
+ std::vector<double> v(N * 100);
+ std::fill(v.begin(), v.end(), 10);
+
+ for (size_t n = 0; n < N; ++n) {
+ std::for_each(v.begin(), v.end(), (
+ ref(sum) += factor * _1
+ ));
+ }
+
+ std::cout << sum << std::endl;
+ return 0;
+}
+

Added: sandbox/local/libs/local/example/benchmark_global_functor.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/benchmark_global_functor.cpp 2011-04-03 13:55:51 EDT (Sun, 03 Apr 2011)
@@ -0,0 +1,34 @@
+
+#include <iostream>
+#include <vector>
+#include <algorithm>
+
+#define N 10000
+
+struct global_add {
+ global_add(double& _sum, int _factor): sum(_sum), factor(_factor) {}
+ void operator()(double num) {
+ sum += factor * num;
+ }
+private:
+ double& sum;
+ const int factor;
+};
+
+int main() {
+ double sum = 0.0;
+ int factor = 10;
+
+ std::vector<double> v(N * 100);
+ std::fill(v.begin(), v.end(), 10);
+
+ global_add add(sum, factor);
+
+ for (size_t n = 0; n < N; ++n) {
+ std::for_each(v.begin(), v.end(), add);
+ }
+
+ std::cout << sum << std::endl;
+ return 0;
+}
+

Added: sandbox/local/libs/local/example/benchmark_local_functor.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/benchmark_local_functor.cpp 2011-04-03 13:55:51 EDT (Sun, 03 Apr 2011)
@@ -0,0 +1,35 @@
+
+#include <iostream>
+#include <vector>
+#include <algorithm>
+
+#define N 10000
+
+int main() {
+ double sum = 0.0;
+ int factor = 10;
+
+ struct local_add {
+ local_add(double& _sum, int _factor): sum(_sum), factor(_factor) {}
+ void operator()(double num) {
+ sum += factor * num;
+ }
+ private:
+ double& sum;
+ const int factor;
+ };
+ local_add add(sum, factor);
+
+ std::vector<double> v(N * 100);
+ std::fill(v.begin(), v.end(), 10);
+
+ for (size_t n = 0; n < N; ++n) {
+ for (size_t i = 0; i < v.size(); ++i) {
+ add(v[i]);
+ }
+ }
+
+ std::cout << sum << std::endl;
+ return 0;
+}
+

Added: sandbox/local/libs/local/example/tparam_trick.cpp
==============================================================================
--- (empty file)
+++ sandbox/local/libs/local/example/tparam_trick.cpp 2011-04-03 13:55:51 EDT (Sun, 03 Apr 2011)
@@ -0,0 +1,53 @@
+
+#include <iostream>
+#include <vector>
+#include <algorithm>
+
+#define N 10000
+
+template<typename R, typename A0>
+struct abstract_function {
+ virtual R operator()(A0) = 0;
+};
+
+template<typename R, typename A0>
+struct function {
+ function(abstract_function<R, A0>& ref): ptr_(&ref) {}
+ R operator()(A0 a0) { return (*ptr_)(a0); }
+private:
+ abstract_function<R, A0>* ptr_;
+};
+
+int main() {
+ double sum = 0.0;
+ int factor = 10;
+
+ struct add_function: abstract_function<void, const double&> {
+ add_function(double& _sum, const int& _factor):
+ sum_(_sum), factor_(_factor) {}
+ void operator()(const double& num) { return body(num, sum_, factor_); }
+ private:
+ double& sum_;
+ const int& factor_;
+ void body(const double& num, double& sum, const int& factor) {
+ sum += factor * num;
+ }
+ };
+ add_function functor_add(sum, factor);
+ function<void, const double&> add(functor_add);
+
+ std::vector<double> v(N * 100);
+ std::fill(v.begin(), v.end(), 10);
+
+ for (size_t n = 0; n < N; ++n) {
+ for (size_t i = 0; i < v.size(); ++i) {
+// functor_add(v[i]); // (1)
+ add(v[i]); // (2)
+ }
+ // std::for_each(v.begin(), v.end(), add); // (3) OK add as tparam!
+ }
+
+ std::cout << sum << std::endl;
+ return 0;
+}
+


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk