. NET4.0 Dynamic VS Reflection efficiency

Source: Internet
Author: User
Keywords Value we this the new feature
Tags class code console continuous it is net net is not only

In my previous article, the continuous promotion. NET4.0 new features. Especially. NET4.0 Dynamic is a new feature. The ensuing problems have also arisen-dynamic.

We do not only need to develop the code concise, can hope that they can write a good structure. It is also very important that we write out the code efficiently. Put aside the implementation principle of dynamic in. net4.0. This article only considers the efficiency of dynamic rather than the use of reflection in the end is fast or slow? Don't. NET to introduce dynamic this new thing reduces our program efficiency? Some netizens point out that commenting on the efficiency of a feature or method, the only criterion is measured.

To get to the point, follow the code below to end your doubts about dynamic efficiency!!!

1, New test class:

public class TestClass
{
public string TestProperty {get; set;}
}

2, the console program to test the efficiency of the Code:

static void Main (string] args)
{
int times = 1000000;

String value = "Dynamic VS Reflection";

Reflection Test begins
TestClass testtypebyreflection = new TestClass ();
Stopwatch Watch1 = Stopwatch.startnew ();
var property = typeof (TestClass). GetProperty ("TestProperty");
for (var i = 0; I < times; i++)
{
Property. SetValue (testtypebyreflection, value, NULL);
}
Console.WriteLine (String. Format ("Reflection time consuming: {0} milliseconds", Watch1.) Elapsedmilliseconds));


Dynamic test Start
Stopwatch WATCH2 = Stopwatch.startnew ();
Dynamic testtypebydynamic = new TestClass ();
for (int i = 0; I < times; i++)
{
Testtypebydynamic.testproperty = value;
}
Console.WriteLine (String. Format ("Dynamic time consuming: {0} milliseconds", Watch2.) Elapsedmilliseconds));

Console.ReadLine ();
}

3, Test TestClass class, start dynamic VS reflection test results are as follows

How, dynamic is faster than reflection?!

Finally hope this article can bring you help, if there are deficiencies are welcome to point out, thank you!

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.