Comparison of the performance of activator. createinstance (type) method for object creation and expression tree creation (new version)

Source: Internet
Author: User
The reason for testing this method is that the project needs to use this method to dynamically create required objects.

The last time we talked about how to create an object using activator. createinstance () and directly using expressions to create an object, the performance comparison was made. I was criticized by Zhao and other experts for the incorrect testing method. I decided to test it again. The testing environment is the same as that for the first time.
TestCode(Form1 is a new winform, and no code is written ):

Code

1 Stopwatch wattings =   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 ());

 

The code for creating an Expression Tree is as follows:

Create Expression Tree and Cache

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)
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 }

The test results are as follows:

Activator. createinstance:
00 : 00 : 00.0140537
Expression. createinstanceex:
00 : 00 : 00.7389382

 

 

Calling, so much difference ......
Let's change the class to try a simple class:

Public   Class Class1 {}

Let's take a look at the test results:

Activator. createinstance
00 : 00 : 00.0020066
Expression. createinstance
00 : 00 : 00.0078841

 

 

Hmm? The result method activator. createinstance () is faster than the expression tree.
To be continued ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.