C#比较dynamic和Dictionary性能

开发中需要传递变参,考虑使用 dynamic 还是 Dictionary(准确地说是Dictionary)。dynamic 的编码体验显著优于 Dictionary,如果性能差距不大的话,我会选择使用dynamic。搜索后没有找到类似对比数据,决定自行实验。

首先使用以下测试代码:

 
 
  1. public void TestDynamic()
  2. {
  3.     var e = CallDynamic(new { Value = 0 });
  4.     int v = e.Value;
  5. }
  6. public void TestDictionary()
  7. {
  8.     var dict = new Dictionary();
  9.     dict["Value"] = 0;
  10.     dict = CallDictionary(dict);
  11.     int v = (int)dict["Value"];
  12. }
  13. private dynamic CallDynamic(dynamic test)
  14. {
  15.     int v = test.Value;
  16.     v++;
  17.     return new { Value = v };
  18. }
  19. private Dictionary CallDictionary(
  20.     Dictionary test)
  21. {
  22.     int v = (int)test["Value"];
  23.     v++;
  24.     var dict = new Dictionary();
  25.     dict["Value"] = v;
  26.     return dict;
  27. }

分别比较运行 1次、10次、100次、1000次、1e4次、1e5次、1e6次 时间
结果:

其中dynamic列和dynamic2列的数据分别是:

在一次运行中执行一步测试 和 在一次运行中连续执行所有测试

分析测试过程和数据,得到以下结论:

1.dynamic***使用会产生一定的性能损耗
2.无论是否***使用,使用次数达到一定量级,dynamic性能一定优于Dictionary
3.一次运行中连续使用dynamic会显著拉低平均性能损耗

考虑到传递变参可能出现多个参数,以上测试不完全。

使用以下代码进行第二阶段实验:

 
 
  1. public void InvokeDynamic()
  2. {
  3.     var e = CallDynamic2(
  4.         new { Value1 = 0, Value2 = 0L, Value3 = 0f, Value4 = 0.0, Value5 = "test" });
  5.     int v1 = e.Value1;
  6.     long v2 = e.Value2;
  7.     float v3 = e.Value3;
  8.     double v4 = e.Value4;
  9.     string v5 = e.Value5;
  10. }
  11. public void InvokeDictionary()
  12. {
  13.     var dict = new Dictionary();
  14.     dict["Value1"] = 0;
  15.     dict["Value2"] = 0L;
  16.     dict["Value3"] = 0f;
  17.     dict["Value4"] = 0.0;
  18.     dict["Value5"] = "test";
  19.     dict = CallDictionary2(dict);
  20.     int v1 = (int)dict["Value1"];
  21.     long v2 = (long)dict["Value2"];
  22.     float v3 = (float)dict["Value3"];
  23.     double v4 = (double)dict["Value4"];
  24.     string v5 = (string)dict["Value5"];
  25. }
  26. private dynamic CallDynamic2(dynamic test)
  27. {
  28.     int v1 = test.Value1;
  29.     long v2 = test.Value2;
  30.     float v3 = test.Value3;
  31.     double v4 = test.Value4;
  32.     string v5 = test.Value5;
  33.     v1++;
  34.     v2++;
  35.     v3++;
  36.     v4++;
  37.     v5 += "test";
  38.     return new { Value1 = v1, Value2 = v2, Value3 = v3, Value4 = v4, Value5 = v5 };
  39. }
  40. private Dictionary CallDictionary2(
  41.     Dictionary test)
  42. {
  43.     int v1 = (int)test["Value1"];
  44.     long v2 = (long)test["Value2"];
  45.     float v3 = (float)test["Value3"];
  46.     double v4 = (double)test["Value4"];
  47.     string v5 = (string)test["Value5"];
  48.     v1++;
  49.     v2++;
  50.     v3++;
  51.     v4++;
  52.     v5 += "test";
  53.     var dict = new Dictionary();
  54.     dict["Value1"] = v1;
  55.     dict["Value2"] = v2;
  56.     dict["Value3"] = v3;
  57.     dict["Value4"] = v4;
  58.     dict["Value5"] = v5;
  59.     return dict;
  60. }

结果数据:

***决定选择使用dynamic

有兄弟考虑可能Box损耗了性能导致Dictionary表现不佳,
专门做了第三阶段实验,对比dynamic和Dictionary

具体数据不贴了,结果是dynamic在100000量级快一倍

网页标题:C#比较dynamic和Dictionary性能
URL链接:http://www.hantingmc.com/qtweb/news30/388230.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联