profile_helpers.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. #ifndef PROFILE_HELPERS_HPP_
  7. #define PROFILE_HELPERS_HPP_
  8. #include <iostream>
  9. #include <cassert>
  10. namespace profile {
  11. void args(int argc, char* argv[], unsigned long& size, unsigned long& trials) {
  12. size = 100000000; // Defaults.
  13. trials = 10; // Default.
  14. if (argc != 1 && argc != 2 && argc != 3) {
  15. std::cerr << "ERROR: Incorrect argument(s)" << std::endl;
  16. std::cerr << "Usage: " << argv[0] << " [SIZE] [TRIALS]" <<
  17. std::endl;
  18. std::cerr << "Defaults: SIZE = " << double(size) << ", TRIALS = " <<
  19. double(trials) << std::endl;
  20. exit(1);
  21. }
  22. if (argc >= 2) size = atol(argv[1]);
  23. if (argc >= 3) trials = atol(argv[2]);
  24. std::clog << "vector size = " << double(size) << std::endl;
  25. std::clog << "number of trials = " << double(trials) << std::endl;
  26. std::clog << "number of calls = " << double(size) * double(trials) <<
  27. std::endl;
  28. }
  29. void display(const unsigned long& size, const unsigned long& trials,
  30. const double& sum, const double& trials_sec,
  31. const double& decl_sec = 0.0) {
  32. std::clog << "sum = " << sum << std::endl;
  33. std::clog << "declaration run-time [s] = " << decl_sec << std::endl;
  34. std::clog << "trials run-time [s] = " << trials_sec << std::endl;
  35. double avg_sec = decl_sec + trials_sec / trials;
  36. std::clog << "average run-time [s] = declaration run-time + trials " <<
  37. "run-time / number of trials = " << std::endl;
  38. std::cout << avg_sec << std::endl; // To cout so it can be parsed easily.
  39. assert(sum == double(size) * double(trials));
  40. }
  41. } // namespace
  42. #endif // #include guard