profile_local_function.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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/local_function.hpp>
  7. #include <boost/chrono.hpp>
  8. #include <vector>
  9. #include <algorithm>
  10. #include <iostream>
  11. #include "profile_helpers.hpp"
  12. int main(int argc, char* argv[]) {
  13. unsigned long size = 0, trials = 0;
  14. profile::args(argc, argv, size, trials);
  15. double sum = 0.0;
  16. int factor = 1;
  17. boost::chrono::system_clock::time_point start =
  18. boost::chrono::system_clock::now();
  19. void BOOST_LOCAL_FUNCTION(
  20. const double& num, bind& sum, const bind& factor) {
  21. sum += factor * num;
  22. } BOOST_LOCAL_FUNCTION_NAME(add)
  23. boost::chrono::duration<double> decl_sec =
  24. boost::chrono::system_clock::now() - start;
  25. std::vector<double> v(size);
  26. std::fill(v.begin(), v.end(), 1.0);
  27. boost::chrono::duration<double> trials_sec;
  28. for(unsigned long i = 0; i < trials; ++i) {
  29. boost::chrono::system_clock::time_point start =
  30. boost::chrono::system_clock::now();
  31. std::for_each(v.begin(), v.end(), add);
  32. trials_sec += boost::chrono::system_clock::now() - start;
  33. }
  34. profile::display(size, trials, sum, trials_sec.count(), decl_sec.count());
  35. return 0;
  36. }