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
14 struct global_add {
global_addglobal_add15 global_add(double& _sum, const int& _factor): sum(_sum), factor(_factor) {}
operator ()global_add16 inline void operator()(const double& num) {
17 sum += factor * num;
18 }
19 private:
20 double& sum;
21 const int& factor;
22 };
23
main(int argc,char * argv[])24 int main(int argc, char* argv[]) {
25 unsigned long size = 0, trials =0;
26 profile::args(argc, argv, size, trials);
27
28 double sum = 0.0;
29 int factor = 1;
30
31 boost::chrono::system_clock::time_point start =
32 boost::chrono::system_clock::now();
33 global_add add(sum, factor);
34 boost::chrono::duration<double> decl_sec =
35 boost::chrono::system_clock::now() - start;
36
37 std::vector<double> v(size);
38 std::fill(v.begin(), v.end(), 1.0);
39
40 boost::chrono::duration<double> trials_sec;
41 for(unsigned long i = 0; i < trials; ++i) {
42 boost::chrono::system_clock::time_point start =
43 boost::chrono::system_clock::now();
44 std::for_each(v.begin(), v.end(), add);
45 trials_sec += boost::chrono::system_clock::now() - start;
46 }
47
48 profile::display(size, trials, sum, trials_sec.count(), decl_sec.count());
49 return 0;
50 }
51
52