再探C#类与结构体究竟谁快考虑栈变量栈分配64位整数密封类.docx
- 文档编号:17598528
- 上传时间:2023-07-27
- 格式:DOCX
- 页数:32
- 大小:23.75KB
再探C#类与结构体究竟谁快考虑栈变量栈分配64位整数密封类.docx
《再探C#类与结构体究竟谁快考虑栈变量栈分配64位整数密封类.docx》由会员分享,可在线阅读,更多相关《再探C#类与结构体究竟谁快考虑栈变量栈分配64位整数密封类.docx(32页珍藏版)》请在冰点文库上搜索。
再探C#类与结构体究竟谁快考虑栈变量栈分配64位整数密封类
再探C#类与结构体究竟谁快——考虑栈变量、栈分配、64位整数、密封类
上次我对C#类与结构体做了一次速度评测(
第一、栈变量。
上次的“硬编码”,是访问类中的静态变量的。
若改为访问函数中的栈变量,性能会不会有所提高?
第二、栈分配(stackalloc)。
既然要测试栈变量,我们还可以顺便测试一下在栈上分配的内存块的访问性能。
第三、64位整数。
由于32位系统的成功,我们已经习惯了使用32位整数(int)。
现在64位系统逐渐普及,我们得为此做好准备。
对于指针操作时经常要用到的偏移量增减运算来说,是使用32位整数,还是使用64位整数,或写两套代码?
这需要测试后才能决定。
第四、密封类(sealed)。
听说密封类能提高性能,我们可以测试一下。
有两种测试方式,一是为原来的派生类增加sealed关键字,二是专门另外写一个密封类。
我决定同时使用这两种方法,分别测试其性能。
一、测试代码
测试代码如下——
?
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Diagnostics;
namespaceTryPointerCall
{
///
///指针操作接口
///
publicinterfaceIPointerCall
{
///
///指针操作
///
///
///
unsafebyte*Ptr(byte*p);
}
#region非泛型
///
///[非泛型]指针操作基类
///
publicabstractclassPointerCall:
IPointerCall
{
publicabstractunsafebyte*Ptr(byte*p);
}
///
///[非泛型]指针操作派生类:
指针+偏移
///
publicsealedclassPointerCallAdd:
PointerCall
{
///
///偏移值
///
publicintOffset=0;
publicoverrideunsafebyte*Ptr(byte*p)
{
returnunchecked(p+Offset);
}
}
///
///[非泛型]指针操作密封类:
指针+偏移
///
publicsealedclassSldPointerCallAdd:
IPointerCall
{
///
///偏移值
///
publicintOffset=0;
publicunsafebyte*Ptr(byte*p)
{
returnunchecked(p+Offset);
}
}
///
///[非泛型]指针操作结构体:
指针+偏移
///
publicstructSPointerCallAdd:
IPointerCall
{
///
///偏移值
///
publicintOffset;
publicunsafebyte*Ptr(byte*p)
{
returnunchecked(p+Offset);
}
}
#endregion
#region泛型
//!
!
!
C#不支持将整数类型作为泛型约束!
!
!
//publicabstractclassGenPointerCall
IPointerCallwhereT:
int,long
//{
// publicabstractunsafebyte*Ptr(byte*p);
// voidd()
// {
// }
//}
#endregion
#region全部测试
///
///指针操作的一些常用函数
///
publicstaticclassPointerCallTool
{
#ifDEBUG
privateconstintCountLoop=10000000;//循环次数
#else
privateconstintCountLoop=200000000; //循环次数
#endif
///
///调用指针操作
///
///
///
///
///
publicstaticunsafebyte*CallPtr
IPointerCall
{
returnptrcall.Ptr(p);
}
publicstaticunsafebyte*CallClassPtr
PointerCall
{
returnptrcall.Ptr(p);
}
publicstaticunsafebyte*CallRefPtr
IPointerCall
{
returnptrcall.Ptr(p);
}
//C#不允许将特定的结构体作为泛型约束。
所以对于结构体只能采用上面那个方法,通过IPointerCall接口进行约束,可能会造成性能下降。
//publicstaticunsafebyte*SCallPtr
SPointerCallAdd
//{
// returnptrcall.Ptr(p);
//}
privatestaticintTryIt_Static_Offset;
privatestaticunsafebyte*TryIt_Static_Ptr(byte*p)
{
returnunchecked(p+TryIt_Static_Offset);
}
///
///执行测试-静态调用
///
///
privatestaticunsafevoidTryIt_Static(StringBuildersOut,intCountLoop)
{
TryIt_Static_Offset=1;
//==性能测试==
byte*p=null;
Stopwatchsw=newStopwatch();
inti;
unchecked
{
#region测试
//硬编码.栈变量
intiOffset=1;
sw.Reset();
sw.Start();
for(i=0;i { p=p+iOffset; } sw.Stop(); sOut.AppendLine(string.Format("硬编码.栈变量: \t{0}",sw.ElapsedMilliseconds)); //硬编码.栈分配 int*pOffset=stackallocint[1]; pOffset[0]=1; sw.Reset(); sw.Start(); for(i=0;i { p=p+pOffset[0]; } sw.Stop(); sOut.AppendLine(string.Format("硬编码.栈分配: \t{0}",sw.ElapsedMilliseconds)); //硬编码.静态 sw.Reset(); sw.Start(); for(i=0;i { p=p+TryIt_Static_Offset; } sw.Stop(); sOut.AppendLine(string.Format("硬编码.静态: \t{0}",sw.ElapsedMilliseconds)); //静态调用 sw.Reset(); sw.Start(); for(i=0;i { p=TryIt_Static_Ptr(p); } sw.Stop(); sOut.AppendLine(string.Format("静态调用: \t{0}",sw.ElapsedMilliseconds)); #endregion//测试 } } privatestaticlongTryIt_Static64_Offset; privatestaticunsafebyte*TryIt_Static64_Ptr(byte*p) { returnunchecked(p+TryIt_Static64_Offset); } /// ///执行测试-静态调用 /// /// privatestaticunsafevoidTryIt_Static64(StringBuildersOut,intCountLoop) { TryIt_Static64_Offset=1; //==性能测试== byte*p=null; Stopwatchsw=newStopwatch(); inti; unchecked { #region测试 //硬编码.栈变量 longiOffset=1; sw.Reset(); sw.Start(); for(i=0;i { p=p+iOffset; } sw.Stop(); sOut.AppendLine(string.Format("64硬编码.栈变量: \t{0}",sw.ElapsedMilliseconds)); //硬编码.栈分配 long*pOffset=stackalloclong[1]; pOffset[0]=1; sw.Reset(); sw.Start(); for(i=0;i { p=p+pOffset[0]; } sw.Stop(); sOut.AppendLine(string.Format("64硬编码.栈分配: \t{0}",
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 再探 C# 结构 究竟 考虑 变量 分配 64 整数 密封