1 
2 // Copyright (C) 2009-2012 Lorenzo Caminiti
3 // Distributed under the Boost Software License, Version 1.0
4 // (see accompanying file LICENSE_1_0.txt or a copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 // Home at http://www.boost.org/libs/local_function
7 
8 #include <boost/chrono.hpp>
9 #include <vector>
10 #include <algorithm>
11 #include <iostream>
12 #include "profile_helpers.hpp"
13 
main(int argc,char * argv[])14 int main(int argc, char* argv[]) {
15     unsigned long size = 0, trials = 0;
16     profile::args(argc, argv, size, trials);
17 
18     double sum = 0.0;
19     int factor = 1;
20 
21     boost::chrono::system_clock::time_point start =
22             boost::chrono::system_clock::now();
23     struct local_add {
24         local_add(double& _sum, const int& _factor):
25                 sum(_sum), factor(_factor) {}
26         inline void operator()(const double& num) {
27             sum += factor * num;
28         }
29     private:
30         double& sum;
31         const int& factor;
32     } add(sum, factor);
33     boost::chrono::duration<double> decl_sec =
34             boost::chrono::system_clock::now() - start;
35 
36     std::vector<double> v(size);
37     std::fill(v.begin(), v.end(), 1.0);
38 
39     boost::chrono::duration<double> trials_sec;
40     for(unsigned long i = 0; i < trials; ++i) {
41         boost::chrono::system_clock::time_point start =
42                 boost::chrono::system_clock::now();
43         for(unsigned long j = 0; j < v.size(); ++j) add(v[j]); // No for_each.
44         trials_sec += boost::chrono::system_clock::now() - start;
45     }
46 
47     profile::display(size, trials, sum, trials_sec.count(), decl_sec.count());
48     return 0;
49 }
50 
51