profile_global_functor.cpp 1.5 KB

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