$include_dir="/home/hyper-archives/boost-commit/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-commit] svn:boost r76351 - sandbox/closure/libs/closure/example
From: lorcaminiti_at_[hidden]
Date: 2012-01-07 18:38:37
Author: lcaminiti
Date: 2012-01-07 18:38:35 EST (Sat, 07 Jan 2012)
New Revision: 76351
URL: http://svn.boost.org/trac/boost/changeset/76351
Log:
Adding some profiling examples.
Added:
   sandbox/closure/libs/closure/example/Jamfile.v2   (contents, props changed)
   sandbox/closure/libs/closure/example/profile_boost_closure.cpp   (contents, props changed)
   sandbox/closure/libs/closure/example/profile_boost_phoenix.cpp   (contents, props changed)
   sandbox/closure/libs/closure/example/profile_helpers.hpp   (contents, props changed)
Properties modified: 
   sandbox/closure/libs/closure/example/   (props changed)
Added: sandbox/closure/libs/closure/example/Jamfile.v2
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/closure/example/Jamfile.v2	2012-01-07 18:38:35 EST (Sat, 07 Jan 2012)
@@ -0,0 +1,17 @@
+
+# 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).
+
+exe profile_boost_closure : profile_boost_closure.cpp :
+    <library>/boost/chrono//boost_chrono
+    <library>/boost/system//boost_system
+    <link>static
+;
+exe profile_boost_phoenix : profile_boost_phoenix.cpp :
+    <library>/boost/chrono//boost_chrono
+    <library>/boost/system//boost_system
+    <link>static
+;
+
Added: sandbox/closure/libs/closure/example/profile_boost_closure.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/closure/example/profile_boost_closure.cpp	2012-01-07 18:38:35 EST (Sat, 07 Jan 2012)
@@ -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).
+
+#include <boost/closure.hpp>
+#include <boost/chrono.hpp>
+#include <vector>
+#include <algorithm>
+#include <iostream>
+#include "profile_helpers.hpp"
+
+int main(int argc, char* argv[]) {
+    unsigned long size = 0, trials = 0;
+    profile::args(argc, argv, size, trials);
+
+    double sum = 0.0;
+    int factor = 1;
+
+    boost::chrono::system_clock::time_point start =
+            boost::chrono::system_clock::now();
+    void BOOST_CLOSURE( (const double& num) (bind& sum) (const bind& factor) ) {
+        sum += factor * num;
+    } BOOST_CLOSURE_END(add)
+    boost::chrono::duration<double> decl_sec =
+            boost::chrono::system_clock::now() - start;
+
+    std::vector<double> v(size);
+    std::fill(v.begin(), v.end(), 1.0);
+
+    boost::chrono::duration<double> trials_sec;
+    for (unsigned long i = 0; i < trials; ++i) {
+        boost::chrono::system_clock::time_point start =
+                boost::chrono::system_clock::now();
+        std::for_each(v.begin(), v.end(), add);
+        trials_sec += boost::chrono::system_clock::now() - start;
+    }
+
+    profile::print(size, trials, sum, trials_sec.count(), decl_sec.count());
+    return 0;
+}
+
Added: sandbox/closure/libs/closure/example/profile_boost_phoenix.cpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/closure/example/profile_boost_phoenix.cpp	2012-01-07 18:38:35 EST (Sat, 07 Jan 2012)
@@ -0,0 +1,44 @@
+
+// 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).
+
+#include <boost/chrono.hpp>
+#include <boost/spirit/home/phoenix/statement/sequence.hpp>
+#include <boost/spirit/home/phoenix/core/reference.hpp>
+#include <boost/spirit/home/phoenix/core/argument.hpp>
+#include <boost/spirit/home/phoenix/operator/arithmetic.hpp>
+#include <iostream>
+#include <vector>
+#include <algorithm>
+#include "profile_helpers.hpp"
+
+int main(int argc, char* argv[]) {
+    unsigned long size = 0, trials = 0;
+    profile::args(argc, argv, size, trials);
+
+    double sum = 0.0;
+    int factor = 1;
+
+    std::vector<double> v(size);
+    std::fill(v.begin(), v.end(), 1.0);
+
+    boost::chrono::duration<double> trials_sec;
+    for (unsigned long i = 0; i < trials; ++i) {
+        boost::chrono::system_clock::time_point start =
+                boost::chrono::system_clock::now();
+
+        using boost::phoenix::ref;
+        using boost::phoenix::arg_names::_1;
+        std::for_each(v.begin(), v.end(), (
+            ref(sum) += factor * _1
+        ));
+
+        trials_sec += boost::chrono::system_clock::now() - start;
+    }
+
+    profile::print(size, trials, sum, trials_sec.count());
+    return 0;
+}
+
Added: sandbox/closure/libs/closure/example/profile_helpers.hpp
==============================================================================
--- (empty file)
+++ sandbox/closure/libs/closure/example/profile_helpers.hpp	2012-01-07 18:38:35 EST (Sat, 07 Jan 2012)
@@ -0,0 +1,53 @@
+
+// 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).
+
+#ifndef PROFILE_HELPERS_HPP_
+#define PROFILE_HELPERS_HPP_
+
+#include <iostream>
+#include <cassert>
+
+namespace profile {
+
+void args(int argc, char* argv[], unsigned long& size, unsigned long& trials) {
+    size = 100000000; // Defaults.
+    trials = 10; // Default.
+    if (argc != 1 && argc != 2 && argc != 3) {
+        std::cerr << "ERROR: Incorrect argument(s)" << std::endl;
+        std::cerr << "Usage: " << argv[0] << " [SIZE] [TRIALS]" <<
+                std::endl;
+        std::cerr << "Defaults: SIZE = " << double(size) << ", TRIALS = " <<
+                double(trials) << std::endl;
+        exit(1);
+    }
+    if (argc >= 2) size = atol(argv[1]);
+    if (argc >= 3) trials = atol(argv[2]);
+
+    std::clog << "vector size = " << double(size) << std::endl;
+    std::clog << "number of trials = " << double(trials) << std::endl;
+    std::clog << "number of calls = " << double(size) * double(trials) <<
+            std::endl;
+}
+
+void print(const unsigned long& size, const unsigned long& trials,
+        const double& sum, const double& trials_sec,
+        const double& decl_sec = 0.0) {
+    std::clog << "sum = " << sum << std::endl;
+    std::clog << "declaration run-time [s] = " << decl_sec << std::endl;
+    std::clog << "trials run-time [s] = " << trials_sec << std::endl;
+
+    double avg_sec = decl_sec + trials_sec / trials;
+    std::clog << "average run-time [s] = declaration run-time + trials " <<
+                "run-time / number of trials = " << std::endl;
+    std::cout << avg_sec << std::endl; // To cout so it can be parsed easily.
+
+    assert(sum == double(size) * double(trials));
+}
+
+} // namespace
+
+#endif // #include guard
+