欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

关注性能:反射操作

程序员文章站 2022-06-25 15:54:59
测试代码 view code  1 using system;  2 using system.collections.generic;  3...

测试代码
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

 关注性能:反射操作

 
 

摘自 学海无涯