C # dynamic application scenarios further explanation

The code is written from the point of view, there is a certain degree of convenience, but also need to factor as the main reference in terms of performance

The following evaluation of the paste phase magnetic properties:

  class Test :ITest{
            public int Calculate(int i)=>i++;
            //static public Test Create()=>new Test();
        }
        interface ITest {
            int Calculate(int i);
        }
        [Test Method]
        public void m22() {
            var dy_checker = new Stopwatch();
            var obj_checker = new Stopwatch();
            var time = 100 * 100000;
            var x2 = typeof(Test);
            ITest w = (ITest)Activator.CreateInstance(x2);
            obj_checker.Start();
            for (int i = 0; i < time; i++) {
                //x2.GetMethod("Calculate").Invoke(w, new object[] { 1 });
                w.Calculate(1);
            }
            obj_checker.Stop();
            dy_checker.Start();
            for (int i = 0; i < time; i++) {
                dynamic x3 = w;
                x3.Calculate(1);
            }
            dy_checker.Stop();
            Console.WriteLine("dy:{0}", dy_checker.ElapsedMilliseconds);
            Console.WriteLine("obj:{0}", obj_checker.ElapsedMilliseconds);
        }
        [Test Method]
        public void m21() {
            var dy_checker = new Stopwatch();
            var obj_checker = new Stopwatch();
            var time = 100 * 100000;
            var x2 = typeof(Test);
            Test w = (Test)Activator.CreateInstance(x2);
            obj_checker.Start();
            for (int i = 0; i < time; i++) {
                x2.GetMethod("Calculate").Invoke(w,new object[]{1 });
            }
            obj_checker.Stop();
            dy_checker.Start();
            for (int i = 0; i < time; i++) {
                dynamic x3 = w;
                x3.Calculate(1);
            }
            dy_checker.Stop();
            Console.WriteLine("dy:{0}", dy_checker.ElapsedMilliseconds);
            Console.WriteLine("obj:{0}", obj_checker.ElapsedMilliseconds);
        }
        [Test Method]
        public void m20() {
            var dy_checker = new Stopwatch();
            var obj_checker = new Stopwatch();
            var time = 100 * 100000;
            obj_checker.Start();
            for (int i = 0; i < time; i++) {
                object x2 = new Test();
                ((Test)x2).Calculate(1);
            }
            obj_checker.Stop();
            dy_checker.Start();
            for (int i = 0; i < time; i++) {
                dynamic x3 = new Test();
                x3.Calculate(1);
            }
            dy_checker.Stop();
            Console.WriteLine("dy:{0}", dy_checker.ElapsedMilliseconds);
            Console.WriteLine("obj:{0}", obj_checker.ElapsedMilliseconds);
        }
        [Test Method]
        public void m19() {
            var dy_checker = new Stopwatch();
            var obj_checker = new Stopwatch();
            var var_checker = new Stopwatch();
            var time = 100 * 100000;
            var_checker.Start ();
            for (int i = 0; i < time; i++) {
                var x3 = 3;
                var x33 = x3 + 5;
            }
            var_checker.Stop();

            obj_checker.Start();
            for (int i = 0; i < time; i++) {
                object x2 = 3;
                var x22 = (int)x2 + 5;
            }
            obj_checker.Stop();
            dy_checker.Start();
            for (int i = 0; i < time; i++) {
                dynamic x1 = 3;
                var x11 = x1 + 5;
            }
            dy_checker.Stop();
            Console.WriteLine("dy:{0}", dy_checker.ElapsedMilliseconds);
            Console.WriteLine("obj:{0}", obj_checker.ElapsedMilliseconds);
            Console.WriteLine("var:{0}", var_checker.ElapsedMilliseconds);
        }
        [Test Method]
        public void m18() {
            var dy_checker = new Stopwatch();
            var obj_checker = new Stopwatch();
            var time=100*100000;
            obj_checker.Start();
            for (int i = 0; i < time; i++) {
                object x2 = 3;
                var x22 = (int)x2 + 5;
            }
            obj_checker.Stop();
            dy_checker.Start();
            for (int i = 0; i < time; i++) {
                dynamic x1 = 3;
                var x11 = x1 + 5;
            }
            dy_checker.Stop();
            Console.WriteLine("dy:{0}",dy_checker.ElapsedMilliseconds);
            Console.WriteLine("obj:{0}",obj_checker.ElapsedMilliseconds);
        }

Test Name: M18
Test Results: Passed
Results StandardOutput:
Dy: 211
obj: 85

 

Test Name: M19
Test Results: Passed
Results StandardOutput:
Dy: 221
obj: 90
var: 22 is

 

Test Name: m20
Test results: Passed
result StandardOutput:
dy: 302
obj: 131

 

Test Name: m21
Test results: Passed
result StandardOutput:
dy: 224
obj: 3057

 

Test Name: m22
Test results: Passed
result StandardOutput:
dy: 229
obj: 43

 ///

Conclusions from the viewpoint of performance, Dynamic object is not good, a single test package from the point of view of the bellows, is 3.5 times the way

So many occasions using the object does not have to be replaced dyanmic

So the above code Test imitate the external add to the mix of the assembly, added during code execution module assembly, such a scenario is very common

In this case two conditions, one is

Oo associated module design, it is stated in the call module in the base, such as base class or interface

The other is not associated oo design, it is the main value of the dynamic manifestation of occasions, because in this case to call this module thing, traditionally only use this root class object by reflecting to call

Comparative tests have been described above, compared with the complexity of the reflection calls, only a simple code on Dynamic and reflection properties only call 1/15 

 

Guess you like

Origin www.cnblogs.com/ProjectDD/p/11318823.html
Recommended