benchmark.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // benchmark.cpp ---------------------------------------------------------------------//
  2. // Copyright Beman Dawes 2011
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // http://www.boost.org/LICENSE_1_0.txt
  5. #ifndef _SCL_SECURE_NO_WARNINGS
  6. # define _SCL_SECURE_NO_WARNINGS
  7. #endif
  8. #ifndef _CRT_SECURE_NO_WARNINGS
  9. # define _CRT_SECURE_NO_WARNINGS
  10. #endif
  11. #include <cstdlib>
  12. #include <boost/endian/conversion.hpp>
  13. #include <boost/random.hpp>
  14. #include <boost/cstdint.hpp>
  15. #include <boost/timer/timer.hpp>
  16. #include <iostream>
  17. #include <string>
  18. using namespace boost;
  19. using std::cout;
  20. using std::cerr;
  21. using std::endl;
  22. using std::vector;
  23. namespace
  24. {
  25. std::string command_args;
  26. long long n_cases;
  27. int places = 2;
  28. bool verbose (false);
  29. #ifndef BOOST_TWO_ARG
  30. typedef int32_t (*timee_func)(int32_t);
  31. #else
  32. typedef void (*timee_func)(int32_t, int32_t&);
  33. #endif
  34. typedef boost::timer::nanosecond_type nanosecond_t;
  35. //--------------------------------------------------------------------------------------//
  36. nanosecond_t benchmark(timee_func timee, const char* msg,
  37. nanosecond_t overhead = 0)
  38. // Returns: total cpu time (i.e. system time + user time)
  39. {
  40. if (verbose)
  41. cout << "\nRunning benchmark..." << endl;
  42. int64_t sum = 0;
  43. boost::timer::cpu_times times;
  44. nanosecond_t cpu_time;
  45. boost::timer::auto_cpu_timer t(places);
  46. for (long long i = n_cases; i; --i)
  47. {
  48. # ifndef BOOST_TWO_ARG
  49. sum += timee(static_cast<int32_t>(i)) ;
  50. # else
  51. int32_t y;
  52. timee(static_cast<int32_t>(i), y);
  53. sum += y;
  54. # endif
  55. }
  56. t.stop();
  57. times = t.elapsed();
  58. cpu_time = (times.system + times.user) - overhead;
  59. const long double sec = 1000000000.0L;
  60. cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
  61. cout.precision(places);
  62. cout << msg << " " << cpu_time / sec << endl;
  63. if (verbose)
  64. {
  65. t.report();
  66. cout << " Benchmark complete\n"
  67. " sum is " << sum << endl;
  68. }
  69. return cpu_time;
  70. }
  71. void process_command_line(int argc, char * argv[])
  72. {
  73. for (int a = 0; a < argc; ++a)
  74. {
  75. command_args += argv[a];
  76. if (a != argc-1)
  77. command_args += ' ';
  78. }
  79. cout << command_args << '\n';;
  80. if (argc >=2)
  81. #ifndef _MSC_VER
  82. n_cases = std::atoll(argv[1]);
  83. #else
  84. n_cases = _atoi64(argv[1]);
  85. #endif
  86. for (; argc > 2; ++argv, --argc)
  87. {
  88. if ( *(argv[2]+1) == 'p' )
  89. places = atoi( argv[2]+2 );
  90. else if ( *(argv[2]+1) == 'v' )
  91. verbose = true;
  92. else
  93. {
  94. cout << "Error - unknown option: " << argv[2] << "\n\n";
  95. argc = -1;
  96. break;
  97. }
  98. }
  99. if (argc < 2)
  100. {
  101. cout << "Usage: benchmark n [Options]\n"
  102. " The argument n specifies the number of test cases to run\n"
  103. " Options:\n"
  104. " -v Verbose messages\n"
  105. " -p# Decimal places for times; default -p" << places << "\n";
  106. return std::exit(1);
  107. }
  108. }
  109. inline void inplace(int32_t& x)
  110. {
  111. x = (static_cast<uint32_t>(x) << 24)
  112. | ((static_cast<uint32_t>(x) << 8) & 0x00ff0000)
  113. | ((static_cast<uint32_t>(x) >> 8) & 0x0000ff00)
  114. | (static_cast<uint32_t>(x) >> 24);
  115. }
  116. inline int32_t by_return(int32_t x)
  117. {
  118. return (static_cast<uint32_t>(x) << 24)
  119. | ((static_cast<uint32_t>(x) << 8) & 0x00ff0000)
  120. | ((static_cast<uint32_t>(x) >> 8) & 0x0000ff00)
  121. | (static_cast<uint32_t>(x) >> 24);
  122. }
  123. inline int32_t by_return_intrinsic(int32_t x)
  124. {
  125. return BOOST_ENDIAN_INTRINSIC_BYTE_SWAP_4(static_cast<uint32_t>(x));
  126. }
  127. inline int32_t by_return_pyry(int32_t x)
  128. {
  129. uint32_t step16;
  130. step16 = static_cast<uint32_t>(x) << 16 | static_cast<uint32_t>(x) >> 16;
  131. return
  132. ((static_cast<uint32_t>(step16) << 8) & 0xff00ff00)
  133. | ((static_cast<uint32_t>(step16) >> 8) & 0x00ff00ff);
  134. }
  135. inline int32_t two_operand(int32_t x, int32_t& y)
  136. {
  137. return y = ((x << 24) & 0xff000000) | ((x << 8) & 0x00ff0000) | ((x >> 24) & 0x000000ff)
  138. | ((x >> 8) & 0x0000ff00);
  139. }
  140. inline int32_t modify_noop(int32_t x)
  141. {
  142. int32_t v(x);
  143. return v;
  144. }
  145. inline int32_t modify_inplace(int32_t x)
  146. {
  147. int32_t v(x);
  148. inplace(v);
  149. return v;
  150. }
  151. inline int32_t modify_by_return(int32_t x)
  152. {
  153. int32_t v(x);
  154. return by_return(v);
  155. }
  156. inline int32_t modify_by_return_pyry(int32_t x)
  157. {
  158. int32_t v(x);
  159. return by_return_pyry(v);
  160. }
  161. inline int32_t modify_by_return_intrinsic(int32_t x)
  162. {
  163. int32_t v(x);
  164. return by_return_intrinsic(v);
  165. }
  166. inline void non_modify_assign(int32_t x, int32_t& y)
  167. {
  168. y = x;
  169. }
  170. inline void non_modify_two_operand(int32_t x, int32_t& y)
  171. {
  172. two_operand(x, y);
  173. }
  174. inline void non_modify_by_return(int32_t x, int32_t& y)
  175. {
  176. y = by_return(x);
  177. }
  178. } // unnamed namespace
  179. //-------------------------------------- main() ---------------------------------------//
  180. int main(int argc, char * argv[])
  181. {
  182. process_command_line(argc, argv);
  183. nanosecond_t overhead;
  184. #ifndef BOOST_TWO_ARG
  185. overhead = benchmark(modify_noop, "modify no-op");
  186. benchmark(modify_inplace, "modify in place"/*, overhead*/);
  187. benchmark(modify_by_return, "modify by return"/*, overhead*/);
  188. benchmark(modify_by_return_pyry, "modify by return_pyry"/*, overhead*/);
  189. benchmark(modify_by_return_intrinsic, "modify by return_intrinsic"/*, overhead*/);
  190. #else
  191. overhead = benchmark(non_modify_assign, "non_modify_assign ");
  192. benchmark(non_modify_two_operand, "non_modify_two_operand", overhead);
  193. benchmark(non_modify_by_return, "non_modify_by_return ", overhead);
  194. #endif
  195. return 0;
  196. }