_c# tutorials for dynamic and dictionary performance comparisons in C #

Source: Internet
Author: User

You need to pass the parameters in development, consider using dynamic or Dictionary (exactly, dictionary<string,object>).

Dynamic's coding experience is significantly better than Dictionary, and if the performance gap is small, I will choose to use dynamic.

After the search did not find similar data, decided to experiment.

First use the following test code:

public void Testdynamic ()
{
var e = calldynamic (new {Value = 0});
int v = e.value;
}
public void Testdictionary () 
{
var dict = new dictionary<string, object> ();
dict["Value"] = 0;
Dict = Calldictionary (dict);
int v = (int) dict["Value"];
}
Private dynamic calldynamic (dynamic test)
{
int v = Test. Value;
v++;
return new {Value = v};
}
Private dictionary<string, object> calldictionary (
dictionary<string, object> test)
{
int v = (int) test["Value"];
v++;
var dict = new dictionary<string, object> ();
dict["Value"] = V;
return dict;
}

Run 1, 10, 100, 1000, 1e4, 1e5, 1e6 times respectively.

Results:

The data for dynamic and Dynamic2 columns are:

Perform one step test in one run and perform all tests continuously in one run

Analyze the test process and data and get the following conclusions:

1.dynamic first use will result in a certain performance loss

2. Whether or not the first use, the number of uses reached a certain level, dynamic performance must be superior to dictionary

3. Continuous use of dynamic in one run can significantly pull down the average performance loss

The above test is incomplete considering that there may be more than one argument for the pass parameter.

Use the following code to perform the second stage experiment:

public void invokedynamic () {var e = CallDynamic2 (new {Value1 = 0, Value2 = 0L, Value3 = 0f, Value4 = 0.0, Value5 = "T
EST "});
int v1 = e.value1;
Long v2 = e.value2;
float v3 = e.value3;
Double v4 = e.value4;
string v5 = E.value5;  public void Invokedictionary () {var dict = new dictionary<string, object> (); dict["Value1"] = 0; dict["Value2"] =
0L;
dict["Value3"] = 0f;
dict["Value4"] = 0.0;
dict["Value5"] = "test";
Dict = CallDictionary2 (dict);
int v1 = (int) dict["Value1"];
Long v2 = (long) dict["Value2"];
FLOAT v3 = (float) dict["Value3"];
Double v4 = (double) dict["Value4"];
String v5 = (string) dict["Value5"]; Private dynamic CALLDYNAMIC2 (dynamic test) {int v1 = Test.
Value1; Long v2 = test.
Value2; FLOAT v3 = test.
Value3; Double v4 = test.
Value4; String v5 = Test.
Value5;
v1++;
v2++;
v3++;
v4++;
V5 + = "Test";
return new {Value1 = v1, Value2 = v2, Value3 = v3, Value4 = v4, Value5 = v5}; Private dictionary<string, object> CallDictionary2 (dictionary<string, Object> Test) {int v1 = (int) test["Value1"]; Long v2 = (long) test["Value2"]; float v3 = (float) test["Value3"]; double v4 = (
Double) test["Value4"];
String v5 = (string) test["Value5"];
v1++;
v2++;
v3++;
v4++;
V5 + = "Test";
var dict = new dictionary<string, object> ();
dict["Value1"] = v1;
dict["Value2"] = v2;
dict["Value3"] = V3;
dict["Value4"] = v4;
dict["Value5"] = v5;
return dict; }

Result data:

Finally decided to choose to use dynamic

Some brothers consider the possibility of box loss of performance caused dictionary performance, specialized in the third phase of the experiment, contrast dynamic and dictionary<string,long>

The exact data is not pasted, the result is dynamic at 100000 magnitude faster than one times

The above is a small set to introduce the C # dynamic and dictionary performance comparison, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.