perf.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #ifndef PERF_HPP
  11. #define PERF_HPP
  12. // this header contains general purpose functions and variables used by
  13. // the boost.compute performance benchmarks.
  14. #include <vector>
  15. #include <cstdlib>
  16. #include <algorithm>
  17. #include <boost/lexical_cast.hpp>
  18. #include <boost/timer/timer.hpp>
  19. static size_t PERF_N = 1024;
  20. static size_t PERF_TRIALS = 3;
  21. // parses command line arguments and sets the corresponding perf variables
  22. inline void perf_parse_args(int argc, char *argv[])
  23. {
  24. if(argc >= 2){
  25. PERF_N = boost::lexical_cast<size_t>(argv[1]);
  26. }
  27. if(argc >= 3){
  28. PERF_TRIALS = boost::lexical_cast<size_t>(argv[2]);
  29. }
  30. }
  31. // generates a vector of random numbers
  32. template<class T>
  33. std::vector<T> generate_random_vector(const size_t size)
  34. {
  35. std::vector<T> vector(size);
  36. std::generate(vector.begin(), vector.end(), rand);
  37. return vector;
  38. }
  39. // a simple timer wrapper which records multiple time entries
  40. class perf_timer
  41. {
  42. public:
  43. typedef boost::timer::nanosecond_type nanosecond_type;
  44. perf_timer()
  45. {
  46. timer.stop();
  47. }
  48. void start()
  49. {
  50. timer.start();
  51. }
  52. void stop()
  53. {
  54. timer.stop();
  55. times.push_back(timer.elapsed().wall);
  56. }
  57. size_t trials() const
  58. {
  59. return times.size();
  60. }
  61. void clear()
  62. {
  63. times.clear();
  64. }
  65. nanosecond_type last_time() const
  66. {
  67. return times.back();
  68. }
  69. nanosecond_type min_time() const
  70. {
  71. return *std::min_element(times.begin(), times.end());
  72. }
  73. nanosecond_type max_time() const
  74. {
  75. return *std::max_element(times.begin(), times.end());
  76. }
  77. boost::timer::cpu_timer timer;
  78. std::vector<boost::timer::nanosecond_type> times;
  79. };
  80. // returns the rate (in MB/s) for processing 'count' items of type 'T'
  81. // in 'time' nanoseconds
  82. template<class T>
  83. double perf_rate(const size_t count, perf_timer::nanosecond_type time)
  84. {
  85. const size_t byte_count = count * sizeof(T);
  86. return (double(byte_count) / 1024 / 1024) / (time / 1e9);
  87. }
  88. #endif // PERF_HPP