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