Performance analysis for using reflection in C #

Source: Internet
Author: User
Tags datetime mscorlib reflection tostring
Performance has recently been studying the framework of a configurable system, the method of reflection is used extensively in the code, although the reflection performance in Java is worse than other languages, but since C # is a strongly typed language, the invocation of the class in AppDomain should not be much worse.
Today at the MVP site to see someone say that the performance of reflection is very poor, to avoid using, it is written a simple example of a test
The test classes are as follows:
Namespace Reflectiontest.test
{
public class Ctester
{
Public Ctester ()
{
A = 10;
}

public void Test1 ()
{
A = (a-0.0001) * 1.0001;
}
Private double A;
Public double Geta () {return A;}
}
}
First we test the structure of the object
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 < + 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 < + j)
{
Type thetest = Type.GetType ("ReflectionTest.Test.CTester");
Object theobj = Thetest.invokemember (null, bindingflags.createinstance
, NULL, NULL, NULL);
}
}

TimeSpan Spand = Datetime.now-now;
Label2. Text = "Time Past" + Spand. ToString ();
}
The test results call the time is about 16ms, while the reflection call is always maintained at 5s 520ms around, direct efficiency is nearly 350 times times more.
The interesting thing about this test is:
If the Type thetest = Type.GetType ("ReflectionTest.Test.CTester") in Test2;
Moves outside the loop, the corresponding running time drops to 1s 332 ms, the efficiency difference is about 20 times times.


Next we tested the member function call:

Test1:

private void Button1_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 < + 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, NULL, NULL);

for (int i = 0; i < 1000; i++)
{
for (int j = 0; J < + 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 ();
}


This example only uses invoke member to test
The preliminary data are as follows:
Test1:10 ms
Test2:2m 53ms

Multiple tests, the resulting data have a slight fluctuation, but the basic ratio remains around 1:250

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

Using ILDASM to view the claimed IL code, it was found that, in addition to the function call, the claimed code was basically consistent, and the difference in visibility performance was
callvirt instance Object [Mscorlib]system.type::invokemember (String,
ValueType [Mscorlib]system.reflection.bindingflags,
class [Mscorlib]system.reflection.binder,
Object
Object[])
caused by the loss of performance caused by reflection.


Although only using InvokeMember to try some simple reflections, it is obvious that the reflection is consuming very much.




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.