Before discussing the interpreted template engine, I decided to share this blog. Because the interpreted engine will introduce the concept of reflection to implement more and more complex functions. Maybe you have started to twenty your facial muscles! In hosting languages, reflection is the most notorious! Its performance is too low, and it is sometimes intolerable. But you don't have to worry about it. Let's talk about how to optimize reflection today!
Overview
The reflection optimization methods involved in this article are as follows:
- Use delegate. createdelegate () to create a delegate for Optimization
- Optimized during dynamic running through. net4
If you still know other more effective optimization methods, please do not hesitate to give us your advice!
Preparations
Today, we have to compare five different methods of calling object members, which is also a performance evaluation.
Before getting started, we first define a simple object and a method for testing:
1 Namespace Reflectionoptimization
2 {
3 Public Sealed Class Testobject
4 {
5 Public Int Add ( Int A, Int B)
6 {
7 // Simple demonstration
8 Return A + B;
9 }
10 }
11 }
This class is very simple and only provides one method. This method returns the sum of two integers. Next let's take a look at the execution time measurementCodeYou must be familiar with it:
1 Private Static Double _ Run ( String Description, Action < Int , Int > Action, Int A, Int B)
2 {
3 If (Action = Null ) Throw New Argumentnullexception ( " Action " );
4
5 // Start Timer
6 VaR Stopwatch = stopwatch. startnew ();
7
8 // Run the code to be measured
9 Action (A, B );
10
11 // End Time
12 Stopwatch. Stop ();
13
14 // Output result
15 Console. writeline ( " {0 }:{ 1} " , Description, stopwatch. elapsed. totalmilliseconds. tostring (cultureinfo. invariantculture ));
16
17 // Return execution time
18 Return Stopwatch. elapsed. totalmilliseconds;
19 }
The preceding measurement time method returns the execution time, because we need to use this value later and take an average value after multiple executions for the fairness and authority of the test.
Coding implementation
First, let's look at the implementation of native reflection:
1 VaROBJ =NewTestobject ();
2 VaRAdd = obj. GetType (). getmethod ("Add");
3
4 For(VaRI =0; I <_ times; I ++) Add. Invoke (OBJ,New Object[] {A, B });
Then let's take a look at the implementation of. net4 dynamic programming:
1Dynamic OBJ =NewTestobject ();
2
3 //Is it super simple to find this code?
4 For(VaRI =0; I <_ times; I ++) obj. Add (A, B );
Finally, let's take a look at how to use delegation to optimize reflection:
1 // Delegate
2 Public Delegate Int Addmethod ( Int A, Int B );
3
4 // Implementation
5 VaR OBJ = New Testobject ();
6 VaR Objtype = obj. GetType ();
7 VaR Add = objtype. getmethod ( " Add " );
8 VaR D = ( Addmethod ) Delegate. createdelegate ( Typeof ( Addmethod ), OBJ, add );
9
10 For ( VaR I = 0 ; I <_ times; I ++) D (A, B );
The above code looks a few more lines and requires a custom delegate, which is quite troublesome to write. Therefore, our test code also implements another form. In fact, it is also a delegate:
VaRD = (func <testobject,Int,Int,Int>) Delegate. createdelegate (Typeof(Func <testobject,Int,Int,Int>), Add );
Test Summary
We first run the entire test code five times in debug mode, then record the average value, and then repeat the test in release mode.
The test process is not described. The test results are as follows:
Debug mode:
Call Method |
First time |
Second |
Third time |
Fourth |
Fifth |
Generic call |
1.022425 |
1.012885 |
0.990775 |
1.020950 |
1.046880 |
Reflection |
147.489220 |
146.012010 |
142.690080 |
139.189335 |
141.663475 |
Dynamic |
9.645850 |
9.979965 |
9.307235 |
9.532665 |
9.730030 |
Func |
1.201860 |
1.214800 |
1.170215 |
1.189280 |
1.239485 |
Delegate |
1.062215 |
1.061635 |
1.067510 |
1.047180 |
1.075190 |
Release mode:
Call Method |
First time |
Second |
Third time |
Fourth |
Fifth |
Generic call |
0.745600 |
0.741365 |
0.722145 |
0.732630 |
0.725645 |
Reflection |
141.778260 |
142.855410 |
142.346095 |
139.649990 |
138.541285 |
Dynamic |
9.631460 |
10.341850 |
9.284230 |
9.457580 |
9.060470 |
Func |
0.882100 |
0.852680 |
0.875695 |
0.854655 |
0.831670 |
Delegate |
0.710280 |
0.722465 |
0.723355 |
0.727175 |
0.693320 |
Comments and conclusions:
- After delegate optimization reflection is used, its performance is almost the same as that of direct calls, which is kept within the same order of magnitude. This solution is recommended when the performance requirements are extremely demanding;
- The performance difference between explicit delegation and anonymous delegation is not obvious, but the performance of explicit delegation is better;
- Native reflection is two orders of magnitude slower than direct calls, and the performance difference has reached 200 times!
- The Dynamic Programming Syntax of. Net 4 is quite concise, and its performance is only one order of magnitude higher than that of direct calls. Because of its concise syntax, we recommend this practice!
- Native reflection technology does not differ much in debug mode and release mode, but other methods have obvious optimization effects (please think about why );
- Although our tests today cannot completely mean that reflection optimization can be comparable to direct calls, but at least those rumors can be defeated to some extent-who says reflection will be slow (giggle )!
Download Code: simple reflection and optimization. Zip