zyl910

优化技巧、硬件体系、图像处理、图形学、游戏编程、国际化与文本信息处理。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  215 随笔 :: 0 文章 :: 145 评论 :: 111万 阅读
< 2025年2月 >
26 27 28 29 30 31 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 1
2 3 4 5 6 7 8

博客代码高亮测试

AS3——

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
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="400" minHeight="300"
               initialize="initApp()" addedToStage="onAddedToStage()">
    <fx:Declarations>
        <!-- 将非可视元素(例如服务、值对象)放在此处 -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import away3d.cameras.*;
            import away3d.core.base.*;
            import away3d.events.*;
            import away3d.materials.*;
            import away3d.primitives.*;
             
            import mx.core.UIComponent;
             
            private var m_world:WorldShow = null;
            private var m_worldCtl:UIComponent = null;
             
            private function initApp():void
            {
                doAdd();
            }
             
            private function onAddedToStage():void
            {
                //doAdd();
                if (null!=stage && null!=m_world)
                {
                    stage.focus = m_world;
                }
            }
             
            private function doAdd():void
            {
                if (null!=m_world) return;
                m_world = new WorldShow();
                m_world.addEventListener(CameraEvent.CAMERA_UPDATED, onCameraUpdated, false, 0, true);
                m_world.addEventListener(WorldShow.SELECTED_CHANGED, onSelectedChanged, false, 0, true);
                 
                //cvsWorld.addChild(pan);
                m_worldCtl = new UIComponent();
                m_worldCtl.addChild(m_world);
                //cvsWorld.addChildAt(m_worldCtl, 0);   // 最底层
                cvsWorld.addElementAt(m_worldCtl, 0);
                 
                if (null!=stage)
                {
                    stage.focus = m_world;
                }
            }
             
            private function doRemove():void
            {
                if (null==m_world)
                    return;
                //
                m_worldCtl.removeChild(m_world);
                //cvsWorld.removeChild(m_worldCtl);
                cvsWorld.removeElement(m_worldCtl);
                m_world = null;
                m_worldCtl = null;
            }
             
            private function doFullScreen():void
            {
                if (stage.displayState == StageDisplayState.NORMAL)
                {
                    stage.displayState = StageDisplayState.FULL_SCREEN;
                }
                else
                {
                    stage.displayState = StageDisplayState.NORMAL;
                }
            }
             
            private function doSetPos():void
            {
                if (null==m_world) return;
                var nx:Number = Number(txtX.text);
                var ny:Number = Number(txtY.text);
                var nz:Number = Number(txtZ.text);
                var nrx:Number = Number(txtRX.text);
                var nry:Number = Number(txtRY.text);
                var nrz:Number = Number(txtRZ.text);
                 
                var camera:Camera3D=m_world.camera;
                camera.rotationX = nrx;
                camera.rotationY = nry;
                camera.rotationZ = nrz;
                camera.position = new Vector3D(nx, ny, nz);
                camera.update();
                //camera.hover(true);
                m_world.notifyCameraUpdate();
            }
             
            // 镜头改变事件
            private function onCameraUpdated(e:CameraEvent):void
            {
                //
                if (null==e)    return;
                var camera:Camera3D = e.camera;
                //trace(camera.x);
                if (null==camera)   return;
                //
                txtX.text = String(camera.x.toFixed(2));
                txtY.text = String(camera.y.toFixed(2));
                txtZ.text = String(camera.z.toFixed(2));
                txtRX.text = String(camera.rotationX.toFixed(2));
                txtRY.text = String(camera.rotationY.toFixed(2));
                txtRZ.text = String(camera.rotationZ.toFixed(2));
                txtfov.text = String(camera.fov.toFixed(2));
                txtfocus.text = String(camera.focus.toFixed(2));
                txtzoom.text = String(camera.zoom.toFixed(2));
            }
             
            private function doSetFov():void
            {
                if (null==m_world) return;
                m_world.camera.fov = Number(txtfov.text);
                m_world.fovDefault = m_world.camera.fov;
                m_world.camera.update();
                m_world.notifyCameraUpdate();
            }
             
            private function doSetFocus():void
            {
                if (null==m_world) return;
                m_world.camera.focus = Number(txtfocus.text);
                var fov:Number = Number(txtfovDef.text);
                if (!isNaN(fov) && fov>0)
                {
                    m_world.camera.update();
                    m_world.camera.fov = fov;
                    m_world.fovDefault = fov;
                }
                else
                {
                    m_world.camera.update();
                    m_world.fovDefault = m_world.camera.fov;
                }
                m_world.camera.update();
                m_world.notifyCameraUpdate();
            }
             
            private function doSetZoom():void
            {
                if (null==m_world) return;
                m_world.camera.zoom = Number(txtzoom.text);
                m_world.camera.update();
                m_world.fovDefault = m_world.camera.fov;
                m_world.camera.update();
                m_world.notifyCameraUpdate();
            }
             
            private function onSelectedChanged(e:MouseEvent3D):void
            {
                if (null==e)    return;
                if (null==e.object) return;
                //trace(e.object.name);
                txtObjName.text = e.object.name;
                txtOX.text = String(e.object.x.toFixed(2));
                txtOY.text = String(e.object.y.toFixed(2));
                txtOZ.text = String(e.object.z.toFixed(2));
                txtORX.text = String(e.object.rotationX.toFixed(2));
                txtORY.text = String(e.object.rotationY.toFixed(2));
                txtORZ.text = String(e.object.rotationZ.toFixed(2));
            }
             
            private function doObjSetPos():void
            {
                if (null==m_world) return;
                if (null==m_world.selected) return;
                var nx:Number = Number(txtOX.text);
                var ny:Number = Number(txtOY.text);
                var nz:Number = Number(txtOZ.text);
                var nrx:Number = Number(txtORX.text);
                var nry:Number = Number(txtORY.text);
                var nrz:Number = Number(txtORZ.text);
                m_world.selected.position = new Vector3D(nx, ny, nz, 1);
            }
             
            protected function chkmoveLockY_clickHandler(event:MouseEvent):void
            {
                // TODO Auto-generated method stub
                m_world.moveLockY = chkmoveLockY.selected;
            }
             
            protected function chklimitCamera_clickHandler(event:MouseEvent):void
            {
                // TODO Auto-generated method stub
                m_world.limitCamera = chklimitCamera.selected;
            }
             
        ]]>
    </fx:Script>
    <s:Group id="cvsWorld" x="0" y="0" width="100%" height="100%">
        <s:Panel id="pnlTool" y="10" right="10" width="316" height="200" backgroundAlpha="0.60"
                 title="WorldShow">
            <mx:TabNavigator width="100%" height="100%" creationPolicy="all" backgroundAlpha="0.10">
                <s:NavigatorContent width="100%" height="100%" label="World">
                    <s:VGroup x="0" y="0" width="100%" height="100%">
                        <s:HGroup width="100%">
                            <s:Button id="btnAdd" label="Add" click="doAdd()"/>
                            <s:Button id="btnRemove" label="Remove" click="doRemove()"/>
                            <s:Button id="btnFullScreen" label="FullScreen" click="doFullScreen()"/>
                        </s:HGroup>
                        <s:HGroup width="100%">
                            <s:CheckBox id="chkmoveLockY" label="moveLockY(Y锁定移动)"
                                        click="chkmoveLockY_clickHandler(event)" selected="true"/>
                            <s:CheckBox id="chklimitCamera" label="limitCamera(限制镜头)"
                                        click="chklimitCamera_clickHandler(event)" selected="true"/>
                        </s:HGroup>
                    </s:VGroup>
                </s:NavigatorContent>
                <s:NavigatorContent width="100%" height="100%" label="Camera">
                    <s:VGroup x="0" y="0" width="100%" height="100%">
                        <s:HGroup width="100%">
                            <s:Button id="btnSetPos" label="SetPos" click="doSetPos()"/>
                        </s:HGroup>
                        <s:HGroup width="100%">
                            <s:Label text="x:"/>
                            <s:TextInput id="txtX" width="80"/>
                            <s:Label text="y:"/>
                            <s:TextInput id="txtY" width="80"/>
                            <s:Label text="z:"/>
                            <s:TextInput id="txtZ" width="80"/>
                        </s:HGroup>
                        <s:HGroup width="100%">
                            <s:Label text="rx:"/>
                            <s:TextInput id="txtRX" width="80"/>
                            <s:Label text="ry:"/>
                            <s:TextInput id="txtRY" width="80"/>
                            <s:Label text="rz:"/>
                            <s:TextInput id="txtRZ" width="80"/>
                        </s:HGroup>
                        <s:HGroup width="100%">
                            <s:Label text="fov:"/>
                            <s:TextInput id="txtfov" width="64"/>
                            <s:Label text="focus:"/>
                            <s:TextInput id="txtfocus" width="64"/>
                            <s:Label text="zoom:"/>
                            <s:TextInput id="txtzoom" width="64"/>
                        </s:HGroup>
                        <s:HGroup width="100%">
                            <s:Button id="btnSetFov" label="set fov" click="doSetFov()"/>
                            <s:Button id="btnSetFocus" label="set focus" click="doSetFocus()"/>
                            <s:TextInput id="txtfovDef" width="80"
                                         prompt="fov默认值。当按下“set foucs”时,同时会修改fov。若该文本框为空,就不修改。"/>
                            <s:Button id="btnSetZoom" label="set zoom" click="doSetZoom()"/>
                        </s:HGroup>
                    </s:VGroup>
                </s:NavigatorContent>
                <s:NavigatorContent width="100%" height="100%" label="Object">
                    <s:VGroup x="0" y="0" width="100%" height="100%">
                        <s:HGroup width="100%">
                            <s:Button id="btnObjSetPos" label="SetPos" click="doObjSetPos()"/>
                            <s:TextInput id="txtObjName" width="100%"/>
                        </s:HGroup>
                        <s:HGroup width="100%">
                            <s:Label text="x:"/>
                            <s:TextInput id="txtOX" width="80"/>
                            <s:Label text="y:"/>
                            <s:TextInput id="txtOY" width="80"/>
                            <s:Label text="z:"/>
                            <s:TextInput id="txtOZ" width="80"/>
                        </s:HGroup>
                        <s:HGroup width="100%">
                            <s:Label text="rx:"/>
                            <s:TextInput id="txtORX" width="80"/>
                            <s:Label text="ry:"/>
                            <s:TextInput id="txtORY" width="80"/>
                            <s:Label text="rz:"/>
                            <s:TextInput id="txtORZ" width="80"/>
                        </s:HGroup>
                    </s:VGroup>
                </s:NavigatorContent>
            </mx:TabNavigator>
        </s:Panel>
    </s:Group>
</s:Application>


C#——

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
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
 
namespace TryPointerCall
{
    /// <summary>
    /// 指针操作接口
    /// </summary>
    public interface IPointerCall
    {
        /// <summary>
        /// 指针操作
        /// </summary>
        /// <param name="p">源指针</param>
        /// <returns>修改后指针</returns>
        unsafe byte* Ptr(byte* p);
    }
 
#region 非泛型
    /// <summary>
    /// [非泛型] 指针操作基类
    /// </summary>
    public abstract class PointerCall : IPointerCall
    {
        public abstract unsafe byte* Ptr(byte* p);
    }
 
    /// <summary>
    /// [非泛型] 指针操作派生类: 指针+偏移
    /// </summary>
    public sealed class PointerCallAdd : PointerCall
    {
        /// <summary>
        /// 偏移值
        /// </summary>
        public int Offset = 0;
 
        public override unsafe byte* Ptr(byte* p)
        {
            return unchecked(p + Offset);
        }
    }
 
    /// <summary>
    /// [非泛型] 指针操作密封类: 指针+偏移
    /// </summary>
    public sealed unsafe class SldPointerCallAdd : IPointerCall
    {
        /// <summary>
        /// 偏移值
        /// </summary>
        public int Offset = 0;
 
        public unsafe byte* Ptr(byte* p)
        {
            return unchecked(p + Offset);
        }
    }
 
    /// <summary>
    /// [非泛型] 指针操作结构体: 指针+偏移
    /// </summary>
    public struct SPointerCallAdd : IPointerCall
    {
        /// <summary>
        /// 偏移值
        /// </summary>
        public int Offset;
 
        public unsafe byte* Ptr(byte* p)
        {
            return unchecked(p + Offset);
        }
    }
 
#endregion
 
#region 泛型
    // !!! C#不支持将整数类型作为泛型约束 !!!
    //public abstract class GenPointerCall<T> : IPointerCall where T: int, long
    //{
    //    public abstract unsafe byte* Ptr(byte* p);
 
    //    void d()
    //    {
    //    }
    //}
 
#endregion
 
#region 全部测试
    /// <summary>
    /// 指针操作的一些常用函数
    /// </summary>
    public static class PointerCallTool
    {
#if DEBUG
        private const int CountLoop = 10000000; // 循环次数
#else
        private const int CountLoop = 200000000;    // 循环次数
#endif
 
        /// <summary>
        /// 调用指针操作
        /// </summary>
        /// <typeparam name="T">具有IPointerCall接口的类型。</typeparam>
        /// <param name="ptrcall">调用者</param>
        /// <param name="p">源指针</param>
        /// <returns>修改后指针</returns>
        public static unsafe byte* CallPtr<T>(T ptrcall, byte* p) where T : IPointerCall
        {
            return ptrcall.Ptr(p);
        }
        public static unsafe byte* CallClassPtr<T>(T ptrcall, byte* p) where T : PointerCall
        {
            return ptrcall.Ptr(p);
        }
        public static unsafe byte* CallRefPtr<T>(ref T ptrcall, byte* p) where T : IPointerCall
        {
            return ptrcall.Ptr(p);
        }
 
        // C#不允许将特定的结构体作为泛型约束。所以对于结构体只能采用上面那个方法,通过IPointerCall接口进行约束,可能会造成性能下降。
        //public static unsafe byte* SCallPtr<T>(T ptrcall, byte* p) where T : SPointerCallAdd
        //{
        //    return ptrcall.Ptr(p);
        //}
 
        private static int TryIt_Static_Offset;
        private static unsafe byte* TryIt_Static_Ptr(byte* p)
        {
            return unchecked(p + TryIt_Static_Offset);
        }
        /// <summary>
        /// 执行测试 - 静态调用
        /// </summary>
        /// <param name="sOut">文本输出</param>
        private static unsafe void TryIt_Static(StringBuilder sOut, int CountLoop)
        {
            TryIt_Static_Offset = 1;
 
            // == 性能测试 ==
            byte* p = null;
            Stopwatch sw = new Stopwatch();
            int i;
            unchecked
            {
                #region 测试
                // 硬编码.栈变量
                int iOffset = 1;
                sw.Reset();
                sw.Start();
                for (i = 0; i < CountLoop; ++i)
                {
                    p = p + iOffset;
                }
                sw.Stop();
                sOut.AppendLine(string.Format("硬编码.栈变量:\t{0}", sw.ElapsedMilliseconds));
 
                // 硬编码.栈分配
                int* pOffset = stackalloc int[1];
                pOffset[0] = 1;
                sw.Reset();
                sw.Start();
                for (i = 0; i < CountLoop; ++i)
                {
                    p = p + pOffset[0];
                }
                sw.Stop();
                sOut.AppendLine(string.Format("硬编码.栈分配:\t{0}", sw.ElapsedMilliseconds));
 
                // 硬编码.静态
                sw.Reset();
                sw.Start();
                for (i = 0; i < CountLoop; ++i)
                {
                    p = p + TryIt_Static_Offset;
                }
                sw.Stop();
                sOut.AppendLine(string.Format("硬编码.静态:\t{0}", sw.ElapsedMilliseconds));
 
                // 静态调用
                sw.Reset();
                sw.Start();
                for (i = 0; i < CountLoop; ++i)
                {
                    p = TryIt_Static_Ptr(p);
                }
                sw.Stop();
                sOut.AppendLine(string.Format("静态调用:\t{0}", sw.ElapsedMilliseconds));
 
                #endregion // 测试
            }
        }
 
        private static long TryIt_Static64_Offset;
        private static unsafe byte* TryIt_Static64_Ptr(byte* p)
        {
            return unchecked(p + TryIt_Static64_Offset);
        }
        /// <summary>
        /// 执行测试 - 静态调用
        /// </summary>
        /// <param name="sOut">文本输出</param>
        private static unsafe void TryIt_Static64(StringBuilder sOut, int CountLoop)
        {
            TryIt_Static64_Offset = 1;
 
            // == 性能测试 ==
            byte* p = null;
            Stopwatch sw = new Stopwatch();
            int i;
            unchecked
            {
                #region 测试
                // 硬编码.栈变量
                long iOffset = 1;
                sw.Reset();
                sw.Start();
                for (i = 0; i < CountLoop; ++i)
                {
                    p = p + iOffset;
                }
                sw.Stop();
                sOut.AppendLine(string.Format("64硬编码.栈变量:\t{0}", sw.ElapsedMilliseconds));
 
                // 硬编码.栈分配
                long* pOffset = stackalloc long[1];
                pOffset[0] = 1;
                sw.Reset();
                sw.Start();
                for (i = 0; i < CountLoop; ++i)
                {
                    p = p + pOffset[0];
                }
                sw.Stop();
                sOut.AppendLine(string.Format("64硬编码.栈分配:\t{0}", sw.ElapsedMilliseconds));
 
                // 硬编码.静态
                sw.Reset();
                sw.Start();
                for (i = 0; i < CountLoop; ++i)
                {
                    p = p + TryIt_Static64_Offset;
                }
                sw.Stop();
                sOut.AppendLine(string.Format("64硬编码.静态:\t{0}", sw.ElapsedMilliseconds));
 
                // 静态调用
                sw.Reset();
                sw.Start();
                for (i = 0; i < CountLoop; ++i)
                {
                    p = TryIt_Static64_Ptr(p);
                }
                sw.Stop();
                sOut.AppendLine(string.Format("64静态调用:\t{0}", sw.ElapsedMilliseconds));
 
                #endregion // 测试
            }
        }
 
        /// <summary>
        /// 执行测试 - 非泛型
        /// </summary>
        /// <param name="sOut">文本输出</param>
        private static unsafe void TryIt_NoGen(StringBuilder sOut, int CountLoop)
        {
            // 创建
            PointerCallAdd pca = new PointerCallAdd();
            SldPointerCallAdd dpca = new SldPointerCallAdd();
            SPointerCallAdd spca;
            pca.Offset = 1;
            spca.Offset = 1;
 
            // 转型
            PointerCall pca_base = pca;
            IPointerCall pca_itf = pca;
            IPointerCall dpca_itf = dpca;
            IPointerCall spca_itf = spca;
 
            // == 性能测试 ==
            byte* p = null;
            Stopwatch sw = new Stopwatch();
            int i;
            unchecked
            {
                #region 调用
                #region 直接调用
                // 调用派生类
                sw.Reset();
                sw.Start();
                for (i = 0; i < CountLoop; ++i)
                {
                    p = pca.Ptr(p);
                }
                sw.Stop();
                sOut.AppendLine(string.Format("调用派生类:\t{0}", sw.ElapsedMilliseconds));
 
                // 调用密封类
                sw.Reset();
                sw.Start();
                for (i = 0; i < CountLoop; ++i)
                {
                    p = dpca.Ptr(p);
                }
                sw.Stop();
                sOut.AppendLine(string.Format("调用密封类:\t{0}", sw.ElapsedMilliseconds));
 
                // 调用结构体
                sw.Reset();
                sw.Start();
                for (i = 0; i < CountLoop; ++i)
                {
                    p = spca.Ptr(p);
                }
                sw.Stop();
                sOut.AppendLine(string.Format("调用结构体:\t{0}", sw.ElapsedMilliseconds));
 
                #endregion  // 直接调用
 
                #region 间接调用
                // 调用基类
                sw.Reset();
                sw.Start();
                for (i = 0; i < CountLoop; ++i)
                {
                    p = pca_base.Ptr(p);
                }
                sw.Stop();
                sOut.AppendLine(string.Format("调用基类:\t{0}", sw.ElapsedMilliseconds));
 
                // 调用派生类的接口
                sw.Reset();
                sw.Start();
                for (i = 0; i < CountLoop; ++i)
                {
                    p = pca_itf.Ptr(p);
                }
                sw.Stop();
                sOut.AppendLine(string.Format("调用派生类的接口:\t{0}", sw.ElapsedMilliseconds));
 
                // 调用密封类的接口
                sw.Reset();
                sw.Start();
                for (i = 0; i < CountLoop; ++i)
                {
                    p = dpca_itf.Ptr(p);
                }
                sw.Stop();
                sOut.AppendLine(string.Format("调用密封类的接口:\t{0}", sw.ElapsedMilliseconds));
 
                // 调用结构体的接口
                sw.Reset();
                sw.Start();
                for (i = 0; i < CountLoop; ++i)
                {
                    p = spca_itf.Ptr(p);
                }
                sw.Stop();
                sOut.AppendLine(string.Format("调用结构体的接口:\t{0}", sw.ElapsedMilliseconds));
 
                #endregion  // 间接调用
 
                #endregion  // 调用
 
                #region 泛型调用
 
                #region 泛型基类约束
                // 基类泛型调用派生类
                sw.Reset();
                sw.Start();
                for (i = 0; i < CountLoop; ++i)
                {
                    p = CallClassPtr(pca, p);
                }
                sw.Stop();
                sOut.AppendLine(string.Format("基类泛型调用派生类:\t{0}", sw.ElapsedMilliseconds));
 
                // 基类泛型调用基类
                sw.Reset();
                sw.Start();
                for (i = 0; i < CountLoop; ++i)
                {
                    p = CallClassPtr(pca_base, p);
                }
                sw.Stop();
                sOut.AppendLine(string.Format("基类泛型调用基类:\t{0}", sw.ElapsedMilliseconds));
 
                #endregion // 泛型基类约束
 
                #region 泛型接口约束 - 直接调用
                // 接口泛型调用派生类
                sw.Reset();
                sw.Start();
                for (i = 0; i < CountLoop; ++i)
                {
                    p = CallPtr(pca, p);
                }
                sw.Stop();
                sOut.AppendLine(string.Format("接口泛型调用派生类:\t{0}", sw.ElapsedMilliseconds));
 
                // 接口泛型调用密封类
                sw.Reset();
                sw.Start();
                for (i = 0; i < CountLoop; ++i)
                {
                    p = CallPtr(dpca, p);
                }
                sw.Stop();
                sOut.AppendLine(string.Format("接口泛型调用密封类:\t{0}", sw.ElapsedMilliseconds));
 
                // 接口泛型调用结构体
                sw.Reset();
                sw.Start();
                for (i = 0; i < CountLoop; ++i)
                {
                    p = CallPtr(spca, p);
                }
                sw.Stop();
                sOut.AppendLine(string.Format("接口泛型调用结构体:\t{0}", sw.ElapsedMilliseconds));
 
                // 接口泛型调用结构体引用
                sw.Reset();
                sw.Start();
                for (i = 0; i < CountLoop; ++i)
                {
                    p = CallRefPtr(ref spca, p);
                }
                sw.Stop();
                sOut.AppendLine(string.Format("接口泛型调用结构体引用:\t{0}", sw.ElapsedMilliseconds));
 
                #endregion  // 直接调用
 
                #region 间接调用
                // 接口泛型调用基类
                sw.Reset();
                sw.Start();
                for (i = 0; i < CountLoop; ++i)
                {
                    p = CallPtr(pca_base, p);
                }
                sw.Stop();
                sOut.AppendLine(string.Format("接口泛型调用基类:\t{0}", sw.ElapsedMilliseconds));
 
                // 接口泛型调用派生类的接口
                sw.Reset();
                sw.Start();
                for (i = 0; i < CountLoop; ++i)
                {
                    p = CallPtr(pca_itf, p);
                }
                sw.Stop();
                sOut.AppendLine(string.Format("接口泛型调用派生类的接口:\t{0}", sw.ElapsedMilliseconds));
 
                // 接口泛型调用密封类的接口
                sw.Reset();
                sw.Start();
                for (i = 0; i < CountLoop; ++i)
                {
                    p = CallPtr(dpca_itf, p);
                }
                sw.Stop();
                sOut.AppendLine(string.Format("接口泛型调用密封类的接口:\t{0}", sw.ElapsedMilliseconds));
 
                // 接口泛型调用结构体的接口
                sw.Reset();
                sw.Start();
                for (i = 0; i < CountLoop; ++i)
                {
                    p = CallPtr(spca_itf, p);
                }
                sw.Stop();
                sOut.AppendLine(string.Format("接口泛型调用结构体的接口:\t{0}", sw.ElapsedMilliseconds));
 
                #endregion  // 间接调用
 
                #endregion  // 泛型调用
 
            }
        }
 
        /// <summary>
        /// 执行测试 - 泛型
        /// </summary>
        /// <param name="sOut">文本输出</param>
        private static unsafe void TryIt_Gen(StringBuilder sOut, int CountLoop)
        {
            // !!! C#不支持将整数类型作为泛型约束 !!!
        }
 
        /// <summary>
        /// 执行测试
        /// </summary>
        public static string TryIt()
        {
            StringBuilder sOut = new StringBuilder();
            sOut.AppendLine("== PointerCallTool.TryIt() ==");
            TryIt_Static(sOut, CountLoop);
            TryIt_Static64(sOut, CountLoop);
            TryIt_NoGen(sOut, CountLoop);
            TryIt_Gen(sOut, CountLoop);
            sOut.AppendLine();
            return sOut.ToString();
        }
 
        /// <summary>
        /// 执行测试 - static
        /// </summary>
        public static string TryItStatic()
        {
            StringBuilder sOut = new StringBuilder();
            int cnt = CountLoop * 10;
            sOut.AppendLine("== PointerCallTool.TryItStatic() ==");
            TryIt_Static(sOut, cnt);
            TryIt_Static64(sOut, cnt);
            sOut.AppendLine();
            return sOut.ToString();
        }
    }
#endregion
 
}

表格——

环境分类基类派生类密封类结构体结构体的引用
A_2005 直接调用 566 563 161 163  
  接口调用   727 721 1045  
  基类约束泛型调用 902 910      
  接口约束泛型调用   1407 1405 566 480
  接口约束泛型调用接口 1409 1410 1402 1617  
A_2010 直接调用 573 568 162 161  
  接口调用   731 730 1134  
  基类约束泛型调用 785 795      
  接口约束泛型调用   733 808 160 161
  接口约束泛型调用接口 728 727 808 1128  
B_2005 直接调用 668 670 101 116  
  接口调用   767 957 1318  
  基类约束泛型调用 1092 1274      
  接口约束泛型调用   1634 1733 671 700
  接口约束泛型调用接口 1767 1702 1719 1859  
B_2010 直接调用 660 668 103 102  
  接口调用   862 862 1340  
  基类约束泛型调用 676 789      
  接口约束泛型调用   862 956 101 98
  接口约束泛型调用接口 764 966 958 1499  
B64_2005 直接调用 675 676 102 191  
  接口调用   862 870 1344  
  基类约束泛型调用 1346 1256      
  接口约束泛型调用   1633 1743 864 769
  接口约束泛型调用接口 1631 1730 1635 2208  
B64_2010 直接调用 577 580 767 772  
  接口调用   770 771 1253  
  基类约束泛型调用 1250 1287      
  接口约束泛型调用   1633 1638 1250 961
  接口约束泛型调用接口 1635 1634 1637 2117  

 

分类 基类 派生类 密封类 结构体 结构体的引用
直接调用 577 580 767 772
接口调用 770 771 1253
基类约束泛型调用 1250 1287
接口约束泛型调用 1633 1638 1250 961
接口约束泛型调用接口 1635 1634 1637 2117

 

(完)

posted on   zyl910  阅读(301)  评论(0编辑  收藏  举报
编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示