speed_test.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // speed_test.cpp --------------------------------------------------------------------//
  2. // Copyright Beman Dawes 2013
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // http://www.boost.org/LICENSE_1_0.txt
  5. //--------------------------------------------------------------------------------------//
  6. //#define BOOST_ENDIAN_NO_INTRINSICS
  7. #include <boost/endian/detail/disable_warnings.hpp>
  8. #include "speed_test_functions.hpp"
  9. #include <boost/endian/conversion.hpp>
  10. #include <boost/endian/arithmetic.hpp>
  11. #include <boost/cstdint.hpp>
  12. #include <boost/timer/timer.hpp>
  13. #include <iostream>
  14. #include <cstdlib>
  15. #include <boost/detail/lightweight_main.hpp>
  16. using namespace boost;
  17. using namespace boost::endian;
  18. using std::cout;
  19. using std::endl;
  20. namespace
  21. {
  22. typedef boost::timer::nanosecond_type nanosecond_t;
  23. std::string command_args;
  24. uint64_t n; // number of test cases to run
  25. int places = 2; // decimal places for times
  26. bool verbose (false);
  27. void process_command_line(int argc, char * argv[])
  28. {
  29. for (int a = 0; a < argc; ++a)
  30. {
  31. command_args += argv[a];
  32. if (a != argc-1)
  33. command_args += ' ';
  34. }
  35. // cout << command_args << '\n';;
  36. if (argc >=2)
  37. #ifndef _MSC_VER
  38. n = atoll(argv[1]);
  39. #else
  40. n = _atoi64(argv[1]);
  41. #endif
  42. for (; argc > 2; ++argv, --argc)
  43. {
  44. if ( *(argv[2]+1) == 'p' )
  45. places = atoi( argv[2]+2 );
  46. else if ( *(argv[2]+1) == 'v' )
  47. verbose = true;
  48. else
  49. {
  50. cout << "Error - unknown option: " << argv[2] << "\n\n";
  51. argc = -1;
  52. break;
  53. }
  54. }
  55. if (argc < 2)
  56. {
  57. cout << "Usage: speed_test n [Options]\n"
  58. " The argument n specifies the number of test cases to run\n"
  59. " Options:\n"
  60. " -v Verbose messages\n"
  61. " -p# Decimal places for times; default -p" << places << "\n";
  62. return std::exit(1);
  63. }
  64. }
  65. //--------------------------------------------------------------------------------------//
  66. template <class T, class EndianT, class Function>
  67. void time(Function f)
  68. {
  69. T x(0);
  70. EndianT y(0);
  71. boost::timer::cpu_timer t;
  72. for (uint64_t i = 0; i < n; ++i)
  73. {
  74. f(x, y);
  75. }
  76. t.stop();
  77. cout << "<td align=\"right\">" << t.format(places, "%t") << " s</td>";
  78. }
  79. void test_big_int16()
  80. {
  81. cout << "<tr><td>16-bit aligned big endian</td>";
  82. time<int16_t, big_int16_t>(user::return_x_big_int16);
  83. time<int16_t, big_int16_t>(user::return_x_value_big_int16);
  84. time<int16_t, big_int16_t>(user::return_x_inplace_big_int16);
  85. time<int16_t, big_int16_t>(user::return_y_big_int16);
  86. cout << "</tr>\n";
  87. }
  88. void test_little_int16()
  89. {
  90. cout << "<tr><td>16-bit aligned little endian</td>";
  91. time<int16_t, little_int16_t>(user::return_x_little_int16);
  92. time<int16_t, little_int16_t>(user::return_x_value_little_int16);
  93. time<int16_t, little_int16_t>(user::return_x_inplace_little_int16);
  94. time<int16_t, little_int16_t>(user::return_y_little_int16);
  95. cout << "</tr>\n";
  96. }
  97. void test_big_int32()
  98. {
  99. cout << "<tr><td>32-bit aligned big endian</td>";
  100. time<int32_t, big_int32_t>(user::return_x_big_int32);
  101. time<int32_t, big_int32_t>(user::return_x_value_big_int32);
  102. time<int32_t, big_int32_t>(user::return_x_inplace_big_int32);
  103. time<int32_t, big_int32_t>(user::return_y_big_int32);
  104. cout << "</tr>\n";
  105. }
  106. void test_little_int32()
  107. {
  108. cout << "<tr><td>32-bit aligned little endian</td>";
  109. time<int32_t, little_int32_t>(user::return_x_little_int32);
  110. time<int32_t, little_int32_t>(user::return_x_value_little_int32);
  111. time<int32_t, little_int32_t>(user::return_x_inplace_little_int32);
  112. time<int32_t, little_int32_t>(user::return_y_little_int32);
  113. cout << "</tr>\n";
  114. }
  115. void test_big_int64()
  116. {
  117. cout << "<tr><td>64-bit aligned big endian</td>";
  118. time<int64_t, big_int64_t>(user::return_x_big_int64);
  119. time<int64_t, big_int64_t>(user::return_x_value_big_int64);
  120. time<int64_t, big_int64_t>(user::return_x_inplace_big_int64);
  121. time<int64_t, big_int64_t>(user::return_y_big_int64);
  122. cout << "</tr>\n";
  123. }
  124. void test_little_int64()
  125. {
  126. cout << "<tr><td>64-bit aligned little endian</td>";
  127. time<int64_t, little_int64_t>(user::return_x_little_int64);
  128. time<int64_t, little_int64_t>(user::return_x_value_little_int64);
  129. time<int64_t, little_int64_t>(user::return_x_inplace_little_int64);
  130. time<int64_t, little_int64_t>(user::return_y_little_int64);
  131. cout << "</tr>\n";
  132. }
  133. } // unnamed namespace
  134. //--------------------------------------------------------------------------------------//
  135. int cpp_main(int argc, char* argv[])
  136. {
  137. process_command_line(argc, argv);
  138. cout
  139. << "<html>\n<head>\n<title>Endian Speed Test</title>\n</head>\n<body>\n"
  140. << "<table border=\"1\" cellpadding=\"5\" cellspacing=\"0\""
  141. << "style=\"border-collapse: collapse\" bordercolor=\"#111111\">\n"
  142. << "<tr><td colspan=\"6\" align=\"center\"><b>"
  143. << BOOST_COMPILER << "</b></td></tr>\n"
  144. << "<tr><td colspan=\"6\" align=\"center\"><b>"
  145. << " Iterations: " << n
  146. << ", Intrinsics: " BOOST_ENDIAN_INTRINSIC_MSG
  147. << "</b></td></tr>\n"
  148. << "<tr><td><b>Test Case</b></td>\n"
  149. "<td align=\"center\"><b>int<br>arg</b></td>\n"
  150. "<td align=\"center\"><b>int<br>value(arg)</b></td>\n"
  151. "<td align=\"center\"><b>int<br>in place(arg)</b></td>\n"
  152. "<td align=\"center\"><b>Endian<br>arg</b></td>\n"
  153. "</tr>\n"
  154. ;
  155. test_big_int16();
  156. test_little_int16();
  157. test_big_int32();
  158. test_little_int32();
  159. test_big_int64();
  160. test_little_int64();
  161. cout << "\n</table>\n</body>\n</html>\n";
  162. return 0;
  163. }
  164. #include <boost/endian/detail/disable_warnings_pop.hpp>