Performance Analysis of Reflection in C # - ZZ

xiaoxiao2021-03-06  111

Recently studied a framework for configurable systems, a large number of reflections in the code, although drawing on other languages, such as reflectivity in Java is relatively poor, but thinking that C # is a strong type of language, for Appdomain The call to the class should be a lot of performance.

Today, I saw someone saying that someone said that the performance of reflection was very poor. To avoid use, I wrote a simple example to test the test class as follows: Namespace ReflectionTest.test {public class ctester {public ctester () = 10 Public void test1 () {a = (a - 0.0001) * 1.0001; PRIVATE DOUBLE A; Public Double Geta () {Return a;}}} First we test the test code for the object's constructed code 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 pas" spand.toT7tring (); private void test2 () {label2.text = "; label4 .Text = ""; datetime now = datetime.now; for (int i = 0; i <1000; i ) {f OR (int J = 0; j <100; 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 pas" spand.tostring ();} Test results are directly called around 16ms, while reflection calls are always maintained at about 52 520ms, direct The efficiency is relatively close to 350 times.

For this test, it is interesting: It is about 20 times.

Next, we tested the member function call: test1: private void button1_click (Object sender, eventargs e) {datetime now = datetime.now; ctester avest = new ctester (); for (int i = 0; i <1000; I ) {for (int J = 0; j <100; j ) {pathest.test1 ();}} Timespan spand = datetime.now - now; label1.text = "time pas" spand.toString (); label3 .Text = "value is now" ateest.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 <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.toTString (); label4.text = "value is now" thewar.geta ();} This example only uses Invoke Member to test the initial data: TEST1: 10 mstest2: 2m 53ms Multiple tests, get The data has slight fluctuations, but the substantially proportion is maintained at 1: 250 for the static method call result for 5ms - 3M 164ms with ILDASM to view the claimed IL code, found that except for function calls,

Claiming that code is consistent, visible performance difference is callvirt instance object [mscorlib] System.Type :: InvokeMember (string, valuetype [mscorlib] System.Reflection.BindingFlags, class [mscorlib] System.Reflection.Binder, object, object []) Caused, that is, the performance loss caused by the reflection. Although only invokemember tries some simple reflections, it is clear that the reflection is very large. Author Blog:

http://blog.9cbs.net/leafwiz/

转载请注明原文地址:https://www.9cbs.com/read-102405.html

New Post(0)