Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does C # compare the performance of dynamic and Dictionary

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

C # how to compare dynamic and Dictionary performance, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

You need to pass parameters in your development, and consider whether to use dynamic or Dictionary (Dictionary to be exact). The coding experience of dynamic is significantly better than that of Dictionary, and if the performance gap is small, I will choose to use dynamic. No similar comparison data was found after the search, so I decided to experiment on my own.

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 (); dict ["Value"] = 0; dict = CallDictionary (dict); int v = (int) dict ["Value"];} private dynamic CallDynamic (dynamic test) {int v = test.Value; vault; return new {Value = v} } private Dictionary CallDictionary (Dictionary test) {int v = (int) test ["Value"]; var dict = new Dictionary (); dict ["Value"] = v; return dict;}

Compare the time of running 1,10,100,1000 times, 1e4 times, 1e5 times and 1e6 times

Results:

The data of the dynamic column and the dynamic2 column are:

Perform one-step tests in one run and continuously execute all tests in one run

Analyze the testing process and data, and draw the following conclusions:

1. The use of dynamicmaterials * will result in some performance loss.

two。 Regardless of whether it is used or not, the performance of dynamic must be better than that of Dictionary when the number of uses reaches a certain order of magnitude.

3. The continuous use of dynamic in one operation will significantly reduce the average performance loss.

Considering that there may be multiple parameters for passing parameters, the above tests are incomplete.

Use the following code for the second phase of the experiment:

Public void InvokeDynamic () {var e = CallDynamic2 (new {Value1 = 0, Value2 = 0L, Value3 = 0f, Value4 = 0.0, Value5 = "test"}); 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 (); dict ["Value2"] = 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 cycles; v2 cycles; v3 cycles; v4 cycles; v5 + = "test"; return new {Value1 = v1, Value2 = v2, Value3 = v3, Value4 = v4, Value5 = v5} } private Dictionary CallDictionary2 (Dictionary 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 () Dict ["Value1"] = v1; dict ["Value2"] = v2; dict ["Value3"] = v3; dict ["Value4"] = v4; dict ["Value5"] = v5; return dict;}

Result data:

Decided to use dynamic

Some brothers consider that the loss of performance of Box may lead to the poor performance of Dictionary.

The third phase of the experiment is specially done to compare dynamic and Dictionary.

The specific data are not posted, and the result is that dynamic is twice as fast as the order of 100000.

After reading the above, have you mastered how C # compares the performance of dynamic and Dictionary? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report