-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
302 lines (263 loc) · 6.59 KB
/
main.cpp
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// ConsoleApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <string>
#include <iostream>
#include <windows.h>
#include <iomanip>
#include <list>
#ifdef DEBUG
const int set_repeats = 10000;
const int tests_repeats = 10;
#else
const int set_repeats = 10000000;
const int tests_repeats = 5;
#endif // DEBUG
const std::string test_str14 = "testtext1234__";
const std::string test_str140 = "testtext1234__testtext1234__testtext1234__testtext1234__testtext1234__testtext1234__testtext1234__testtext1234__testtext1234__testtext1234__";
const std::string test_str7 = "tstx13_";
const std::string & test_str = test_str140;
const int test_str_size = test_str.size();
class myClass
{
std::string stra;
long long counter; //use counter to not let compiler optimize repeated calls
public:
void setStrByValue(std::string _str)
{
stra = _str;
counter++;
}
void setStrByValueWithMove(std::string _str)
{
stra = std::move(_str);
counter++;
}
void setStrByReference(const std::string& _str)
{
stra = _str;
counter++;
}
void setStrByReferenceWithMove(const std::string& _str)
{
stra = std::move(_str);
counter++;
}
void setStrByRValue(std::string&& _str)
{
stra = _str;
counter++;
}
void setStrByRValueWithMove(std::string&& _str)
{
stra = std::move(_str);
counter++;
}
void setStrByPointer(const std::string* _str_ptr)
{
stra = *_str_ptr;
counter++;
}
void setStrBySharedPtr(const std::shared_ptr<std::string> _str_ptr)
{
stra = *_str_ptr;
counter++;
}
myClass()
{
counter = 0;
}
inline void beforeSet()
{
resetStr();
if (test_str.size() != test_str_size && stra.size() == 0)
{
std::cout << "failed set size check test_str = " << test_str.size() << std::endl;
exit(0);
}
}
inline void afterSet()
{
if (stra.size() != test_str_size)
{
std::cout << "failed set size check stra = " << stra.size() << std::endl;
exit(0);
}
}
inline void resetStr()
{
stra.clear();
}
long long getCounter() { return counter; };
};
inline void test1_setStrByValue(myClass & myObj)
{
for (int i = 0; i < set_repeats; i++)
{
myObj.beforeSet();
myObj.setStrByValue(test_str);
myObj.afterSet();
}
}
inline void test2_setStrByValueWithMove(myClass & myObj)
{
for (int i = 0; i < set_repeats; i++)
{
myObj.beforeSet();
myObj.setStrByValueWithMove(test_str);
myObj.afterSet();
}
}
inline void test3_setStrByReference(myClass & myObj)
{
for (int i = 0; i < set_repeats; i++)
{
myObj.beforeSet();
myObj.setStrByReference(test_str);
myObj.afterSet();
}
}
inline void test4_setStrByRValue(myClass & myObj)
{
for (int i = 0; i < set_repeats; i++)
{
myObj.beforeSet();
myObj.setStrByRValue(std::string(test_str));
myObj.afterSet();
}
}
inline void test5_setStrByRValueWithMove(myClass & myObj)
{
for (int i = 0; i < set_repeats; i++)
{
myObj.beforeSet();
myObj.setStrByRValueWithMove(std::string(test_str));
myObj.afterSet();
}
}
inline void test6_setStrByReferenceWithMove(myClass & myObj)
{
for (int i = 0; i < set_repeats; i++)
{
myObj.beforeSet();
myObj.setStrByReferenceWithMove(test_str);
myObj.afterSet();
}
}
inline void test7_setStrByPointerPtr(myClass & myObj)
{
for (int i = 0; i < set_repeats; i++)
{
myObj.beforeSet();
myObj.setStrByPointer(&test_str);
myObj.afterSet();
}
}
inline void test8_setStrBySharedPtr(myClass & myObj)
{
auto ptr = std::make_shared<std::string>(test_str);
for (int i = 0; i < set_repeats; i++)
{
myObj.beforeSet();
myObj.setStrBySharedPtr(ptr);
myObj.afterSet();
}
}
void testForVSProfiler()
{
myClass myObj;
for (int i = 0; i < tests_repeats; i++)
{
test3_setStrByReference(myObj);
test1_setStrByValue(myObj);
test2_setStrByValueWithMove(myObj);
test4_setStrByRValue(myObj);
test5_setStrByRValueWithMove(myObj);
test6_setStrByReferenceWithMove(myObj);
test7_setStrByPointerPtr(myObj);
test8_setStrBySharedPtr(myObj);
}
std::cout << "final counter = " << myObj.getCounter() << std::endl;
}
void print_ticks(const std::string & name, const std::list<int> &ticks)
{
int ticks_sum = 0;
int ticks_zeroes = 0;
int ticks_max = 0;
for (int tick : ticks)
{
ticks_sum += tick;
if (tick == 0)
{
ticks_zeroes++;
}
if (ticks_max == 0 || ticks_max < tick)
{
ticks_max = tick;
}
}
std::cout << "ticks in " << std::setw(32) << name << " = " << std::setw(5) << ticks_sum << ", zeroes = " << std::setw(3) << ticks_zeroes << " , max " << std::setw(5) << ticks_max << std::endl;
}
void testByTimings()
{
myClass myObj;
DWORD ticksend;
DWORD ticksstart;
std::list< int> test1_ticks;
std::list< int> test2_ticks;
std::list< int> test3_ticks;
std::list< int> test4_ticks;
std::list< int> test5_ticks;
std::list< int> test6_ticks;
std::list< int> test7_ticks;
std::list< int> test8_ticks;
for (int i = 0; i < tests_repeats; i++)
{
ticksstart = GetTickCount();
test1_setStrByValue(myObj);
ticksend = GetTickCount();
test1_ticks.push_back(ticksend - ticksstart);
ticksstart = GetTickCount();
test2_setStrByValueWithMove(myObj);
ticksend = GetTickCount();
test2_ticks.push_back(ticksend - ticksstart);
ticksstart = GetTickCount();
test3_setStrByReference(myObj);
ticksend = GetTickCount();
test3_ticks.push_back(ticksend - ticksstart);
ticksstart = GetTickCount();
test4_setStrByRValue(myObj);
ticksend = GetTickCount();
test4_ticks.push_back(ticksend - ticksstart);
ticksstart = GetTickCount();
test5_setStrByRValueWithMove(myObj);
ticksend = GetTickCount();
test5_ticks.push_back(ticksend - ticksstart);
ticksstart = GetTickCount();
test6_setStrByReferenceWithMove(myObj);
ticksend = GetTickCount();
test6_ticks.push_back(ticksend - ticksstart);
ticksstart = GetTickCount();
test7_setStrByPointerPtr(myObj);
ticksend = GetTickCount();
test7_ticks.push_back(ticksend - ticksstart);
ticksstart = GetTickCount();
test8_setStrBySharedPtr(myObj);
ticksend = GetTickCount();
test8_ticks.push_back(ticksend - ticksstart);
}
print_ticks("test1_setStrByValue", test1_ticks);
print_ticks("test2_setStrByValueWithMove", test2_ticks);
print_ticks("test3_setStrByReference", test3_ticks);
print_ticks("test4_setStrByRValue", test4_ticks);
print_ticks("test5_setStrByRValueWithMove", test5_ticks);
print_ticks("test6_setStrByReferenceWithMove", test6_ticks);
print_ticks("test7_setStrByPointerPtr", test7_ticks);
print_ticks("test8_setStrBySharedPtr", test8_ticks);
std::cout << "final counter = " << myObj.getCounter() << std::endl;
}
int main()
{
std::cout << "tests_repeats=" << tests_repeats << ", set_repeats=" << set_repeats << std::endl;
//testForVSProfiler();
testByTimings();
}