Performance Analysis Using Reflection in C #

Source: Internet
Author: User
Tags mscorlib

Recently, I have been studying a configurable system framework. reflection methods are widely used in the Code. Although reflection performance in other languages such as java is relatively poor, however, since c # is a strongly typed language, the calling performance of classes in AppDomain should not be much inferior.

Today, on the mvp site, someone said that the reflection performance is poor. To avoid using it, I wrote a simple example to test it.

The test class is as follows:

Namespace ReflectionTest. Test
{
Public class CTester
{
Public CTester ()
{
A = 10;
}
Public void test1 ()
{
A = (a-0.0001) * 1.0001;
}
Private double;
Public double geta () {return ;}
}
}
 
First, we test the object structure.
The test code is as follows:

Private void test1 ()
{
Label1.Text = "";
Label3.Text = "";
DateTime now = DateTime. Now;
For (int I = 0; I <1000; I ++)
{
For (int j = 0; j <100; j ++)
{

CTester aTest = new CTester ();
}
}

TimeSpan spand = DateTime. Now-now;
Label1.Text = "time past" + spand. ToString ();
}

Private void test2 ()
{
Label2.Text = "";
Label4.Text = "";
DateTime now = DateTime. Now;

For (int I = 0; I <1000; I ++)
{
For (int j = 0; j <100; j ++)
{
Type theTest = Type. GetType ("ReflectionTest. Test. CTester ");
Object theobj = theTest. InvokeMember (null, BindingFlags. CreateInstance
, Null );
}
}
TimeSpan spand = DateTime. Now-now;
Label2.Text = "time past" + spand. ToString ();
}

The direct call time of the test result is about 16 ms, while the reflection call is always around 5 s 350 ms, and the direct efficiency is close to times.
One interesting thing about this test is:
If you set Type theTest = Type. GetType ("ReflectionTest. Test. CTester") in test2 ");
Moving out of the loop, the corresponding running time is reduced to 1 s 332 MS, the efficiency difference is about 20 times.

Next we will test the member function call:

Test1:
Private void button#click (object sender, EventArgs e)
{
DateTime now = DateTime. Now;

CTester aTest = new CTester ();
For (int I = 0; I <1000; I ++)
{
For (int j = 0; j <100; j ++)
{
  
ATest. test1 ();
}
}

TimeSpan spand = DateTime. Now-now;
Label1.Text = "time past" + spand. ToString ();
Label3.Text = "value is now" + aTest. geta ();
}

Test2:

Private void button2_Click (object sender, EventArgs e)
{
DateTime now = DateTime. Now;

Type theTest = Type. GetType ("ReflectionTest. Test. CTester ");
Object theobj = theTest. InvokeMember (null, BindingFlags. CreateInstance
, Null );
For (int I = 0; I <1000; I ++)
{
For (int j = 0; j <100; j ++)
{
  
TheTest. InvokeMember ("test1", BindingFlags. InvokeMethod, null, theobj, new object [0]);
}
}
CTester thewar = theobj as CTester;

TimeSpan spand = DateTime. Now-now;
Label2.Text = "time past" + spand. ToString ();
Label4.Text = "value is now" + thewar. geta ();
}
 
In this example, only invoke member is used for testing.
The initial data is as follows:
MS test1: 10
Test2: 2 m 53 ms

After multiple tests, the data obtained experienced slight fluctuations, but basically the ratio remained around.

For static method calls
The result is 5 ms-3 m 164 ms.

Using ILDASM to view the claimed IL code, we found that except for function calls, the claimed code is basically the same. The difference in performance is

Callvirt instance object [mscorlib] System. Type: InvokeMember (string,
Valuetype [mscorlib] System. Reflection. BindingFlags,
Class [mscorlib] System. Reflection. Binder,
Object,
Object [])

This is the performance loss caused by reflection.

Although only invokemember tried some simple reflection, it is obvious that reflection consumption is very large.

Related Article

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.