文章有误,等待晚上的新版本。。。
对于
对于
public static Object CreateInstance (
Type type
)
Type type
)
方法我想您应该用过,当您需要动态的创建某些类型实例的时候您就需要它了。但是我今天测试该方法和用Expression Tree方法实例化对象的哪个执行的效率更快的时候,我发现一个问题。我把问题代码抽出来,做几个实验,您看看:(该实验在vs2008和vs2005下面都做了,实验结果一样,具体数值是vs2005下面的,以下代码均是在Release模式下)
没有直接实例化的
1 Stopwatch watch1 = new Stopwatch();
2 watch1.Start();
3 Class1 formActivator1 = Activator.CreateInstance(typeof(Class1)) as Class1;
4 watch1.Stop();
5 Console.WriteLine("Activator CreateInstance");
6 Console.WriteLine(watch1.Elapsed.ToString());
7
2 watch1.Start();
3 Class1 formActivator1 = Activator.CreateInstance(typeof(Class1)) as Class1;
4 watch1.Stop();
5 Console.WriteLine("Activator CreateInstance");
6 Console.WriteLine(watch1.Elapsed.ToString());
7
得出的记过为:
Activator CreateInstance
00:00:00.0001561
00:00:00.0001561
我在前面加上如下代码:
Class1 c1 = new Class1();
再执行,我们看看结果:
Activator CreateInstance
00:00:00.0000203
00:00:00.0000203
您看到区别了吗?没有Class1 c1 = new Class1();的时候执行时间是有Class1 c1 = new Class1();执行时间的将近8倍。那么我们再看下面的代码:
1 Class1 c1 = new Class1();
2
3 Stopwatch watch1 = new Stopwatch();
4 watch1.Start();
5 Class1 formActivator1 = Activator.CreateInstance(typeof(Class1)) as Class1;
6 watch1.Stop();
7 Console.WriteLine("Activator CreateInstance");
8 Console.WriteLine(watch1.Elapsed.ToString());
9
10 Stopwatch watch2 = new Stopwatch();
11 watch2.Start();
12 Class1 formActivator2 = Activator.CreateInstance(typeof(Class1)) as Class1;
13 watch2.Stop();
14 Console.WriteLine("Activator CreateInstance");
15 Console.WriteLine(watch2.Elapsed.ToString());
2
3 Stopwatch watch1 = new Stopwatch();
4 watch1.Start();
5 Class1 formActivator1 = Activator.CreateInstance(typeof(Class1)) as Class1;
6 watch1.Stop();
7 Console.WriteLine("Activator CreateInstance");
8 Console.WriteLine(watch1.Elapsed.ToString());
9
10 Stopwatch watch2 = new Stopwatch();
11 watch2.Start();
12 Class1 formActivator2 = Activator.CreateInstance(typeof(Class1)) as Class1;
13 watch2.Stop();
14 Console.WriteLine("Activator CreateInstance");
15 Console.WriteLine(watch2.Elapsed.ToString());
我们再一次执行方法Activator.CreateInstance(),您能想到执行结果吗?
Activator CreateInstance
00:00:00.0000206
Activator CreateInstance
00:00:00.0001156
00:00:00.0000206
Activator CreateInstance
00:00:00.0001156
吆喝,这个结果真是怪异啊!那么再来看这个:
把实例化放在中间
1 Stopwatch watch1 = new Stopwatch();
2 watch1.Start();
3 Class1 formActivator1 = Activator.CreateInstance(typeof(Class1)) as Class1;
4 watch1.Stop();
5 Console.WriteLine("Activator CreateInstance");
6 Console.WriteLine(watch1.Elapsed.ToString());
7
8 Class1 c1 = new Class1();
9
10 Stopwatch watch2 = new Stopwatch();
11 watch2.Start();
12 Class1 formActivator2 = Activator.CreateInstance(typeof(Class1)) as Class1;
13 watch2.Stop();
14 Console.WriteLine("Activator CreateInstance");
15 Console.WriteLine(watch2.Elapsed.ToString());
2 watch1.Start();
3 Class1 formActivator1 = Activator.CreateInstance(typeof(Class1)) as Class1;
4 watch1.Stop();
5 Console.WriteLine("Activator CreateInstance");
6 Console.WriteLine(watch1.Elapsed.ToString());
7
8 Class1 c1 = new Class1();
9
10 Stopwatch watch2 = new Stopwatch();
11 watch2.Start();
12 Class1 formActivator2 = Activator.CreateInstance(typeof(Class1)) as Class1;
13 watch2.Stop();
14 Console.WriteLine("Activator CreateInstance");
15 Console.WriteLine(watch2.Elapsed.ToString());
您注意第八行,我们把实例化的代码放在中间了,那么结果又会怎么样呢?您请看:
Activator CreateInstance
00:00:00.0001715
Activator CreateInstance
00:00:00.0001111
00:00:00.0001715
Activator CreateInstance
00:00:00.0001111
和您预想的结果一样吗?
以下的疑问您能解答吗?
1. Activator.CreateInstance到底是怎么实例化对象的,和new有什么区别呢?
2. 为什么前面加上对象的直接实例化就会出现执行效率上的区别呢?
3. 为什么把直接实例化放在中间就没有相应的效果了呢?
4. 既然这样,那么我直接实例化两次对象用方法Activator.CreateInstance(),会不会第二次的执行时间要比第一次少很多呢?(我的实验结果,没有影响)