几种线程本地存储变量和普通变量的性能比较

这次要优化的是libGod库中的线程本地存储变量,线程本地存储变量访问非常频繁,优化后库的性能应该会提高不少。已知的线程本地存储方法有boost中的thread_specific_ptr类,gcc中的__thread关键字,pthread中的pthread_getspecific函数。这次测试这3中本地存储以及普通变量之间的性能差别,代码如下:

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
#include <iostream>   
#include <stdio.h>
#include <pthread.h>
#include <boost/thread/thread.hpp>
#include <boost/thread/tss.hpp>

using namespace std;

class C {
public:
C(int a) {
m_a = a;
printf("C() %d\n", m_a);
}
~C() {
printf("~C() %d\n", m_a);
}

private:
int m_a;
};

#define TM 3

#if TM == 1
boost::thread_specific_ptr<C> pc;
const char *testType = "boost";
#elif TM == 2
__thread C *pc;
const char *testType = "__thread";
#elif TM == 3
pthread_key_t pc;
const char *testType = "pthread";
#else
C *pc;
const char *testType = "normal";
#endif

void boostthreadFunc() {
#if TM == 1
pc.reset(new C(10));
#elif TM == 2
pc = new C(20);
#elif TM == 3
if (pthread_key_create(&pc, NULL)) {
cout << "pthread_key_create" << endl;
return;
}
if (pthread_setspecific(pc, new C(30))) {
cout << "pthread_setspecific" << endl;
return;
}
#else
pc = new C(20);
#endif
int switches = 5000000;
int i = switches;
struct timeval tm_start, tm_end;
gettimeofday(&tm_start, NULL);
while (i--) {
#if TM == 1
C *c1 = pc.get();
C *c2 = pc.get();
C *c3 = pc.get();
C *c4 = pc.get();
C *c5 = pc.get();

C *c6 = pc.get();
C *c7 = pc.get();
C *c8 = pc.get();
C *c9 = pc.get();
C *c10 = pc.get();

C *c11 = pc.get();
C *c12 = pc.get();
C *c13 = pc.get();
C *c14 = pc.get();
C *c15 = pc.get();

C *c16 = pc.get();
C *c17 = pc.get();
C *c18 = pc.get();
C *c19 = pc.get();
C *c20 = pc.get();

C *c21 = pc.get();
C *c22 = pc.get();
C *c23 = pc.get();
C *c24 = pc.get();
C *c25 = pc.get();

C *c26 = pc.get();
C *c27 = pc.get();
C *c28 = pc.get();
C *c29 = pc.get();
C *c30 = pc.get();

C *c31 = pc.get();
C *c32 = pc.get();
C *c33 = pc.get();
C *c34 = pc.get();
C *c35 = pc.get();

C *c36 = pc.get();
C *c37 = pc.get();
C *c38 = pc.get();
C *c39 = pc.get();
C *c40 = pc.get();

#elif TM == 2
C *c1 = pc;
C *c2 = pc;
C *c3 = pc;
C *c4 = pc;
C *c5 = pc;

C *c6 = pc;
C *c7 = pc;
C *c8 = pc;
C *c9 = pc;
C *c10 = pc;

C *c11 = pc;
C *c12 = pc;
C *c13 = pc;
C *c14 = pc;
C *c15 = pc;

C *c16 = pc;
C *c17 = pc;
C *c18 = pc;
C *c19 = pc;
C *c20 = pc;

C *c21 = pc;
C *c22 = pc;
C *c23 = pc;
C *c24 = pc;
C *c25 = pc;

C *c26 = pc;
C *c27 = pc;
C *c28 = pc;
C *c29 = pc;
C *c30 = pc;

C *c31 = pc;
C *c32 = pc;
C *c33 = pc;
C *c34 = pc;
C *c35 = pc;

C *c36 = pc;
C *c37 = pc;
C *c38 = pc;
C *c39 = pc;
C *c40 = pc;

#elif TM == 3
C *c1 = (C *)pthread_getspecific(pc);
C *c2 = (C *)pthread_getspecific(pc);
C *c3 = (C *)pthread_getspecific(pc);
C *c4 = (C *)pthread_getspecific(pc);
C *c5 = (C *)pthread_getspecific(pc);

C *c6 = (C *)pthread_getspecific(pc);
C *c7 = (C *)pthread_getspecific(pc);
C *c8 = (C *)pthread_getspecific(pc);
C *c9 = (C *)pthread_getspecific(pc);
C *c10 = (C *)pthread_getspecific(pc);

C *c11 = (C *)pthread_getspecific(pc);
C *c12 = (C *)pthread_getspecific(pc);
C *c13 = (C *)pthread_getspecific(pc);
C *c14 = (C *)pthread_getspecific(pc);
C *c15 = (C *)pthread_getspecific(pc);

C *c16 = (C *)pthread_getspecific(pc);
C *c17 = (C *)pthread_getspecific(pc);
C *c18 = (C *)pthread_getspecific(pc);
C *c19 = (C *)pthread_getspecific(pc);
C *c20 = (C *)pthread_getspecific(pc);

C *c21 = (C *)pthread_getspecific(pc);
C *c22 = (C *)pthread_getspecific(pc);
C *c23 = (C *)pthread_getspecific(pc);
C *c24 = (C *)pthread_getspecific(pc);
C *c25 = (C *)pthread_getspecific(pc);

C *c26 = (C *)pthread_getspecific(pc);
C *c27 = (C *)pthread_getspecific(pc);
C *c28 = (C *)pthread_getspecific(pc);
C *c29 = (C *)pthread_getspecific(pc);
C *c30 = (C *)pthread_getspecific(pc);

C *c31 = (C *)pthread_getspecific(pc);
C *c32 = (C *)pthread_getspecific(pc);
C *c33 = (C *)pthread_getspecific(pc);
C *c34 = (C *)pthread_getspecific(pc);
C *c35 = (C *)pthread_getspecific(pc);

C *c36 = (C *)pthread_getspecific(pc);
C *c37 = (C *)pthread_getspecific(pc);
C *c38 = (C *)pthread_getspecific(pc);
C *c39 = (C *)pthread_getspecific(pc);
C *c40 = (C *)pthread_getspecific(pc);

#else
C *c1 = pc;
C *c2 = pc;
C *c3 = pc;
C *c4 = pc;
C *c5 = pc;

C *c6 = pc;
C *c7 = pc;
C *c8 = pc;
C *c9 = pc;
C *c10 = pc;

C *c11 = pc;
C *c12 = pc;
C *c13 = pc;
C *c14 = pc;
C *c15 = pc;

C *c16 = pc;
C *c17 = pc;
C *c18 = pc;
C *c19 = pc;
C *c20 = pc;

C *c21 = pc;
C *c22 = pc;
C *c23 = pc;
C *c24 = pc;
C *c25 = pc;

C *c26 = pc;
C *c27 = pc;
C *c28 = pc;
C *c29 = pc;
C *c30 = pc;

C *c31 = pc;
C *c32 = pc;
C *c33 = pc;
C *c34 = pc;
C *c35 = pc;

C *c36 = pc;
C *c37 = pc;
C *c38 = pc;
C *c39 = pc;
C *c40 = pc;

#endif
}
gettimeofday(&tm_end, NULL);
switches *= 40;
long long ns = (tm_end.tv_sec - tm_start.tv_sec) * 1000LL * 1000LL * 1000LL +
(tm_end.tv_usec - tm_start.tv_usec) * 1000LL;
std::cout << "####Benchmark result#### " << testType << std::endl;
std::cout << "Totol switches : " << switches << std::endl;
std::cout << "Cost per switch(ns) : " << (double)ns/switches << std::endl;
std::cout << "All cost switch(ns) : " << ns << std::endl;
std::cout << "####Benchmark result####" << std::endl;
}

int main() {
boost::thread bt(&boostthreadFunc);
bt.join();
printf("main exit..\n");
return 0;
}

结果:

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
a@pc:~/code$ ./boost_tls  
C() 10
####Benchmark result#### boost
Totol switches : 200000000
Cost per switch(ns) : 53.8458
All cost switch(ns) : 10769165000
####Benchmark result####
~C() 10
main exit..
a@pc:~/code$ ./__thread_tls
C() 20
####Benchmark result#### __thread
Totol switches : 200000000
Cost per switch(ns) : 0.888435
All cost switch(ns) : 177687000
####Benchmark result####
main exit..
a@pc:~/code$ ./pthread_tls
C() 30
####Benchmark result#### pthread
Totol switches : 200000000
Cost per switch(ns) : 10.0046
All cost switch(ns) : 2000914000
####Benchmark result####
main exit..
a@pc:~/code$ ./normal
C() 20
####Benchmark result#### normal
Totol switches : 200000000
Cost per switch(ns) : 0.886275
All cost switch(ns) : 177255000
####Benchmark result####
main exit..

由此可见,boost是最慢的,pthread中的比boost快了约5倍,__thread最快,性能和普通变量差不多。

文章目录