【问题标题】:Execution time takes 30+ minutes looping through two lists of Dictionaries执行时间需要 30 多分钟循环遍历两个字典列表
【发布时间】:2012-06-04 04:12:38
【问题描述】:

目前我的程序是这样工作的:

List<Dict<string, double>> GroupA;
List<Dict<string, double>> GroupB;
* All dictionaries have the same keys but with dif values

foreach(string key in GroupA[0])
{
      List<double> GroupA_Values;
      foreach(Dict d in GroupA)
          GroupA_Values.add(d[key]);

      List<double> GroupB_Values;
      foreach(Dict d in GroupB)
          GroupB_Values.add(d[key]);

      CompareValuesFunction(GroupA_Values, GroupB_Values);
}

有没有更有效的方法?

【问题讨论】:

  • 这与“编译时间”无关。
  • 这不是有效的 C# 代码。如果我们能看到您实际在做什么会很有帮助,因为它很可能会对性能产生影响。
  • CompareValuesFunction 有什么作用?在使用它们之前,您是否必须将值从字典中复制出来?
  • 您可以使用GroupA.ValuesGroupB.Values 而不是将值复制到新的List&lt;double&gt; 中。另外,要回答您的问题,您需要告诉我们每个字典中有多少条目,然后向我们展示CompareValuesFunction() 代码。
  • 第一个 foreach 在那里做什么?如果你只是在做 groupA[0] .. 我不明白这一点?

标签: c# list sorting dictionary compare


【解决方案1】:

您应该有一个Dictionary&lt;string, List&lt;string&gt;&gt;,而不是一个字典列表,其中的值是与特定键关联的所有值的列表。

目前,您正在为每个字典值处理每个字典中的每个值。这根本不能很好地扩展。通过将所有值复制到列表中,所有内容都将线性缩放,而不是多项式。

这种设置也将消耗更少的内存(字典比其他数据结构消耗更多的内存以获得快速的搜索速度)。

正如评论中提到的,如果我们对 CompareValuesFunction 的内容有更多了解(即查看它的代码),我们或许可以提出其他改进建议。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    相关资源
    最近更新 更多