-
Notifications
You must be signed in to change notification settings - Fork 2
/
swaptest.cc
284 lines (255 loc) · 6.56 KB
/
swaptest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <algorithm>
#include <functional>
#include <iostream>
#include <cstdint>
#include <string>
#include <memory>
#include "random.h"
#include "slice.h"
using namespace std;
template <typename DType>
inline void Bentleyswap(DType*x,int i, int j)
{
DType t = x[i];
x[i] = x[j];
x[j] = t;
}
template <typename _RanIt>
void generate_test_datas(_RanIt _First, _RanIt _Last, int)
{
typedef iterator_traits<_RanIt>::value_type value_t;
value_t i = 0;
while (_First != _Last)
*_First++ = i++;
}
void generate_test_datas(string* first,string*last,int len)
{
random rand(16807);
while(first < last){
random_string(rand,len,*first++);
}
}
template<typename T,int N>
void generate_test_datas(slice<T,N>* first,slice<T,N>*last,int len)
{
random rand(16807);
while(first < last){
random_string(rand,len,*first++);
}
}
template <typename _RanIt>
void test_iter_swap(_RanIt _First, _RanIt _Last)
{ // [_Frist,_Last)
while(_First < _Last)
std::iter_swap(_First++,--_Last);
}
template <typename _RanIt,typename _Ty>
int test_compare(_RanIt _First, _RanIt _Last, const _Ty& val)
{ // [_Frist,_Last)
register int temp = 0;
while(_First < _Last){
if(*_First++ < val)
temp++;
}
return temp;
}
template <typename DType>
void test_Bentleyswap(DType*x,int i, int u)
{ // [l,u]
while (i < u)
Bentleyswap(x, i++, u--);
}
const int maxIter = 100;
enum method_t
{
itertor_swap = 0,
array_swap,
value_compare,
max_method_count,
};
const char* methodName[max_method_count] =
{
" iter_swap",
"array_swap",
" compare "
};
double timesPerRun[max_method_count][maxIter];
template <typename VType>
static void bench(int iter,int size,int VLen = 0)
{
VType * data = (VType *) malloc(sizeof(VType) * size);
std::uninitialized_fill(data, data+size, VType());
generate_test_datas(data,data+size,VLen);
VType tmp = data[size/2];
const int allRuns = iter * max_method_count;
int curentRun = 0;
int xxx;
for (int iter_i = 0 ; iter_i < iter; iter_i++)
for(int i = 0; i < max_method_count;i++){
cerr << "run [" << ++curentRun << '/' << allRuns << "]";
clock_t t1 = clock();
switch (i)
{
case itertor_swap:
test_iter_swap(data,data+size);
test_iter_swap(data,data+size);
break;
case array_swap:
test_Bentleyswap(data,0,size-1);
test_Bentleyswap(data,0,size-1);
break;
case value_compare:
xxx = test_compare(data,data+size,tmp);
break;
}
clock_t t2 = clock();
if(i == value_compare)
cerr <<" = "<< xxx ;
cerr <<'\n';
timesPerRun[i][iter_i]= (double)(t2-t1)/CLOCKS_PER_SEC;
}
free(data);
}
#define delim ','
int main(int argc, char *argv[])
{
int N = 50000000;
int Iter = 1;
if (argc < 3)
fprintf(stderr, "Usage: %s [%d] [%d]\n", argv[0], N,Iter);
if (argc > 1) N = atoi(argv[1]);
if (argc > 2) Iter = atoi(argv[2]);
Iter = min(Iter,maxIter);
cout<< " type "<<delim
<< "method" <<delim
<< " time\n";
bench<int32_t>(Iter,N);
for (int j = 0; j < Iter;j++)
for(int i = 0;i < max_method_count;i++){
cout<<"int32_t"<<delim
<<methodName[i]<<delim
<<timesPerRun[i][j]
<<'\n';
}
bench<int64_t>(Iter,N);
for (int j = 0; j < Iter;j++)
for(int i = 0;i < max_method_count;i++){
cout<<"int64_t"<<delim
<<methodName[i]<<delim
<<timesPerRun[i][j]
<<'\n';
}
bench<double>(Iter,N);
for (int j = 0; j < Iter;j++)
for(int i = 0;i < max_method_count;i++){
cout<<"double"<<delim
<<methodName[i]<<delim
<<timesPerRun[i][j]
<<'\n';
}
//bench<string>(Iter,N,12);
//for (int j = 0; j < Iter;j++)
// for(int i = 0;i < max_method_count;i++){
// cout<<"string 12" <<delim
// <<methodName[i]<<delim
// <<timesPerRun[i][j]
// <<'\n';
// }
//bench<string>(Iter,N,16);
//for (int j = 0; j < Iter;j++)
// for(int i = 0;i < max_method_count;i++){
// cout<<"string 16" <<delim
// <<methodName[i]<<delim
// <<timesPerRun[i][j]
// <<'\n';
// }
//bench<string>(Iter,N,32);
//for (int j = 0; j < Iter;j++)
// for(int i = 0;i < max_method_count;i++){
// cout<<"string 32" <<delim
// <<methodName[i]<<delim
// <<timesPerRun[i][j]
// <<'\n';
// }
//bench<string>(Iter,N,64);
//for (int j = 0; j < Iter;j++)
// for(int i = 0;i < max_method_count;i++){
// cout<<"string 64" <<delim
// <<methodName[i]<<delim
// <<timesPerRun[i][j]
// <<'\n';
// }
//bench<string>(Iter,N,128);
//for (int j = 0; j < Iter;j++)
// for(int i = 0;i < max_method_count;i++){
// cout<<"string 128" <<delim
// <<methodName[i]<<delim
// <<timesPerRun[i][j]
// <<'\n';
// }
//bench<string>(Iter,N,256);
//for (int j = 0; j < Iter;j++)
// for(int i = 0;i < max_method_count;i++){
// cout<<"string 256" <<delim
// <<methodName[i]<<delim
// <<timesPerRun[i][j]
// <<'\n';
// }
typedef slice<char, 12> sliceChar_12;
typedef slice<char, 32> sliceChar_32;
typedef slice<char, 64> sliceChar_64;
typedef slice<char, 128> sliceChar_128;
typedef slice<char, 256> sliceChar_256;
bench<sliceChar_12>(Iter,N,12);
for (int j = 0; j < Iter;j++)
for(int i = 0;i < max_method_count;i++){
cout<<"12" <<delim
<<methodName[i]<<delim
<<timesPerRun[i][j]
<<'\n';
}
bench<sliceChar_16>(Iter,N,16);
for (int j = 0; j < Iter;j++)
for(int i = 0;i < max_method_count;i++){
cout<<"16" <<delim
<<methodName[i]<<delim
<<timesPerRun[i][j]
<<'\n';
}
bench<sliceChar_32>(Iter,N,32);
for (int j = 0; j < Iter;j++)
for(int i = 0;i < max_method_count;i++){
cout<<"32" <<delim
<<methodName[i]<<delim
<<timesPerRun[i][j]
<<'\n';
}
bench<sliceChar_64>(Iter,N,64);
for (int j = 0; j < Iter;j++)
for(int i = 0;i < max_method_count;i++){
cout<<"64" <<delim
<<methodName[i]<<delim
<<timesPerRun[i][j]
<<'\n';
}
bench<sliceChar_128>(Iter,N,128);
for (int j = 0; j < Iter;j++)
for(int i = 0;i < max_method_count;i++){
cout<<"128" <<delim
<<methodName[i]<<delim
<<timesPerRun[i][j]
<<'\n';
}
bench<sliceChar_256>(Iter,N,256);
for (int j = 0; j < Iter;j++)
for(int i = 0;i < max_method_count;i++){
cout<<"256" <<delim
<<methodName[i]<<delim
<<timesPerRun[i][j]
<<'\n';
}
}