迭戈

博客园 首页 新随笔 联系 管理
  之所以要测试这个方法,是因为项目中要使用该方法动态创建需要对象。  

  上次说了方法Activator.CreateInstance()创建对象和直接用表达式创建对象的性能比较。被老赵等大牛批评测试方法不对,我决定再测试一下,测试环境和第一次一样。
  测试代码如下(其中Form1就是新建的一个WinForm,什么代码都没有写):
代码
 1 Stopwatch watch1 = new Stopwatch();
 2 watch1.Start();
 3 for (int i = 0; i < 10000; i++)
 4 {
 5      Form1 form1 = Activator.CreateInstance(typeof(Form1)) as Form1;
 6 }
 7 
 8 watch1.Stop();
 9 Console.WriteLine("Activator.CreateInstance");
10 Console.WriteLine(watch1.Elapsed.ToString());
11 
12 watch1.Reset();
13 watch1.Start();
14 for (int i = 0; i < 10000; i++)
15 {
16      Form form1 = GetCache.InstanceCacheEx.InstanceCache(typeof(Form1)) as Form1;
17 }
18 watch1.Stop();
19 Console.WriteLine("Expression.CreateInstanceEx");
20 Console.WriteLine(watch1.Elapsed.ToString());

 

而创建Expression Tree的代码如下:
创建Expression Tree,并缓存
 1     public class InstanceCachesEx
 2     {
 3         private Dictionary<Type, Func<object>> dicEx = new Dictionary<Type, Func<object>>();
 4         public object InstanceCache(Type key)
 5         {
 6 
 7             object value = null;
 8 
 9             if (dicEx.TryGetValue(key, out value))
10             {
11                 return value();
12             }
13             else
14             {
15                 value = CreateInstance(key);
16                 dicEx[key] = value;
17                 return value();
18             }
19         }
20 
21         static Func<object> CreateInstance(Type type)
22         {
23             NewExpression newExp = Expression.New(type);
24             Expression<Func<object>> lambdaExp = Expression.Lambda<Func<object>>(newExp, null);
25             Func<object> func = lambdaExp.Compile();
26             return func;
27         }
28     }
29 
30     public static class GetCache
31     {
32         static GetCache()
33         {
34             InstanceCacheEx = new InstanceCachesEx();
35         }
36 
37         public static InstanceCachesEx InstanceCacheEx { getset; }
38     }

测试结果如下:

Activator.CreateInstance:
00:00:00.0140537
Expression.CreateInstanceEx:
00:00:00.7389382

 

 

吆呼,差了这么多......
我们换一个类来试试,来一个简单的类:

public class Class1 { }

再来看看测试结果:

Activator.CreateInstance
00:00:00.0020066
Expression.CreateInstance
00:00:00.0078841

 

 

嗯?结果方法Activator.CreateInstance()比表达式树要快了。
待续......

posted on 2009-12-07 22:11  Will Meng  阅读(7042)  评论(13编辑  收藏  举报