当前位置:编程学习 > asp >>

关注性能:反射操作

测试代码
View Code

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Reflection;
 6
 7 namespace CSharpStudy
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             {
14                 dynamic dynamicObject = new StringBuilder();
15
16                 Helper.Watch(() =>
17                 {
18                     for (var i = 0; i < 1000000; i++)
19                     {
20                         dynamicObject.Append("hello");
21                     }
22                 });
23             }
24
25             {
26                 var appendMethod = typeof(StringBuilder).GetMethod("Append", new Type[] { typeof(string) });
27                 var sb = new StringBuilder();
28
29                 Helper.Watch(() =>
30                 {
31                     for (var i = 0; i < 1000000; i++)
32                     {
33                         appendMethod.Invoke(sb, new object[] { "hello" });
34                     }
35                 });
36             }
37
38             {
39                 var appendMethod = typeof(StringBuilder).GetMethod("Append", new Type[] { typeof(string) });
40                 var sb = new StringBuilder();
41                 var appendAction = (Func<string, StringBuilder>)Delegate.CreateDelegate(typeof(Func<string, StringBuilder>), sb, appendMethod);
42
43                 Helper.Watch(() =>
44                 {
45                     for (var i = 0; i < 1000000; i++)
46                     {
47                         appendAction("hello");
48                     }
49                 });
50             }
51         }
52     }
53

 \

 
 

摘自 学海无涯

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,