Activator.CreateInstance(Type type)方法创建对象和Expression Tree创建对象性能的比较(终结版)
2009-12-08 22:02
513 查看
这应该是这个文章的最终版本了,期间犯了不少错误,也学习到了不少东西。
不多说了,直接上代码。还是我最后的测试结果,代码很简单,不解释了。
1 Stopwatch watch1 = new Stopwatch();
2
3 watch1.Start();
4 for (int i = 0; i < 1000000; i++)
5 {
6 Activator.CreateInstance(typeof(Class1));
7 }
8
9 watch1.Stop();
10 Console.WriteLine("Activator.CreateInstance");
11 Console.WriteLine(watch1.Elapsed.ToString());
12
13 watch1.Reset();
14 watch1.Start();
15 for (int i = 0; i < 1000000; i++)
16 {
17 GetCache.InstanceCacheEx.InstanceCache(typeof(Class1));
18 }
19 watch1.Stop();
20 Console.WriteLine("Expression.CreateInstanceEx");
21 Console.WriteLine(watch1.Elapsed.ToString());
22
23
24 watch1.Reset();
25 watch1.Start();
26 for (int i = 0; i < 1000000; i++)
27 {
28 new Class1();
29 }
30 watch1.Stop();
31 Console.WriteLine("Direct CreateInstance");
32 Console.WriteLine(watch1.Elapsed.ToString());
表达式树构建和缓存:
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 Func<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 { get; set; }
38 }
测试结果:
Activator.CreateInstance:
00:00:00.2221068
Expression.CreateInstanceEx:
00:00:00.0845797
Direct CreateInstance
00:00:00.0071313
之所以这么测试,是因为Activator.CreateInstance()方法使用了缓存机制,所以对于表达式树创建对象也使用了缓存,没有新东西,只是一个总结而已。至于为什么Activator.CreateInstance方法创建对象比用表达式创建对象要慢,您可以看这篇文章(友情提示:或许评论能带给你更多的东西呦)
不多说了,直接上代码。还是我最后的测试结果,代码很简单,不解释了。
1 Stopwatch watch1 = new Stopwatch();
2
3 watch1.Start();
4 for (int i = 0; i < 1000000; i++)
5 {
6 Activator.CreateInstance(typeof(Class1));
7 }
8
9 watch1.Stop();
10 Console.WriteLine("Activator.CreateInstance");
11 Console.WriteLine(watch1.Elapsed.ToString());
12
13 watch1.Reset();
14 watch1.Start();
15 for (int i = 0; i < 1000000; i++)
16 {
17 GetCache.InstanceCacheEx.InstanceCache(typeof(Class1));
18 }
19 watch1.Stop();
20 Console.WriteLine("Expression.CreateInstanceEx");
21 Console.WriteLine(watch1.Elapsed.ToString());
22
23
24 watch1.Reset();
25 watch1.Start();
26 for (int i = 0; i < 1000000; i++)
27 {
28 new Class1();
29 }
30 watch1.Stop();
31 Console.WriteLine("Direct CreateInstance");
32 Console.WriteLine(watch1.Elapsed.ToString());
表达式树构建和缓存:
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 Func<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 { get; set; }
38 }
测试结果:
Activator.CreateInstance:
00:00:00.2221068
Expression.CreateInstanceEx:
00:00:00.0845797
Direct CreateInstance
00:00:00.0071313
之所以这么测试,是因为Activator.CreateInstance()方法使用了缓存机制,所以对于表达式树创建对象也使用了缓存,没有新东西,只是一个总结而已。至于为什么Activator.CreateInstance方法创建对象比用表达式创建对象要慢,您可以看这篇文章(友情提示:或许评论能带给你更多的东西呦)
相关文章推荐
- 再谈Activator.CreateInstance(Type type)方法创建对象和Expression Tree创建对象性能的比较(更新版)
- 探究.net对象的创建,质疑《再谈Activator.CreateInstance(Type type)方法创建对象和Expression Tree创建对象性能的比较》
- Activator.CreateInstance 方法创建对象和Expression Tree创建对象性能的比较(构造函数含多参数的情况)
- Activator.CreateInstance 方法创建对象和Expression Tree创建对象性能的比较(构造函数含多参数的情况)
- Activator.CreateInstance 方法 (Type)
- Activator.CreateInstance 方法 (Type) 的用法
- 动态创建菜单,menustrip,根据权限显示菜单,控制菜单可用,反射,给窗体传值,反射对象传值,public static Object CreateInstance ( Type type, params Object[] args )
- 动态创建菜单,menustrip,根据权限显示菜单,控制菜单可用,反射,给窗体传值,反射对象传值,public static Object CreateInstance ( Type type, pa
- Activator.CreateInstance 方法 (Type) 的用法
- Activator.CreateInstance 方法 (Type)和Class1 obj=new Class1()方法实例化
- [转载内容]动态创建菜单,menustrip,根据权限显示菜单,控制菜单可用,反射,给窗体传值,反射对象传值,public static Object CreateInstance ( Type type, params Object[] args )
- Activator.CreateInstance 方法 (Type) 的用法
- Activator.CreateInstance 方法 (Type) 的用法
- 41-知识补充(UIPickerView的补充方法,使用self创建对象的好处,id和instancetype区别,KVC底层代码实现,文本框的两个代理方法)
- Activator.CreateInstance 反射实例化对象
- 关于反射中Assembly.CreateInstance()与Activator.CreateInstance()方法的区别
- 笔记 Activator.CreateInstance(Type)
- 编写一个手机类(Mobile),包括手机品牌(brand)、手机型号(type), 方法包括显示手机信息,并编写测试类进行对象的创建
- 【知识库】--spring 创建bean的实例-createBeanInstance方法解析(240)
- C# Activator.CreateInstance()方法使用