关注性能:反射操作
测试代码
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
摘自 学海无涯
上一篇: 关注性能:字符串操作
下一篇: 带着新人看java虚拟机02