【问题标题】:What is faster and more convenient: Hashtable or Dictionary<int, double>()?哪个更快更方便:Hashtable 还是 Dictionary<int, double>()?
【发布时间】:2011-04-29 12:23:25
【问题描述】:

序言:我正在处理产生大型数据数组的重载应用程序。

我写了下面这个类

    using System;
    using System.Collections;
    using System.Collections.Generic;

    namespace CSharpSampleApplication.Data.CoreObjects
    {
        [Serializable]
        public class CalcItem
        {
            public CalcItem()
            {
                _additional = new Hashtable();
            }

            private readonly Hashtable _additional;

            public bool ContainsKey(int id)
            {
                return _additional.ContainsKey(id);
            }

            public void Add(int id, double value)
            {
                _additional.Add(id, value);
            }

            public DateTime Date { get; set; }

            public object this[int id]
            {
                get
                {
                    return _additional[id];
                }
            }
        }


    }

然后,在另一个类中,我创建了包含以下内容的管理器:

    public List<CalcItem> CalcItems{ get; private set;}
    private readonly Dictionary<string, int> _keys;
    private int _index;
    private readonly object  _lock = new object();

    public int GetIndex(string key)
    {
        lock (_lock)
        {
            if (_keys.ContainsKey(key))
                return _keys[key];
            else
            {
                _index++;
                _keys.Add(key, _index);
                return _index;
            }
        }
    }

通过使用这些类,我记录了一些实时数据,例如:

                var clc = new CalcItem();
                clc.Date = DateTime.Now;
                clc.Add(_calcItemManager.GetIndex("testData"), r.Next() / 100.00);
                clc.Add(_calcItemManager.GetIndex("testData1"), r.Next() / 100.00);

                i++;

                if (i % 25 == 0)
                {
                    clc.Add(_calcItemManager.GetIndex("testData2"), r.Next()/100.00);
                    clc.Add(_calcItemManager.GetIndex("testData3"), r.Next()/100.00);
                    clc.Add(_calcItemManager.GetIndex("testData4"), r.Next()/100.00);
                    clc.Add(_calcItemManager.GetIndex("testData5"), r.Next()/100.00);
                }
                _calcItemManager.Add(clc);

所以管理器存储了所有 calcItems 的 [string key]-[int index] 绑定。

问题是: 使用Dictionary&lt;int, double&gt; 而不是 Hashtable() 来优化内存使用和更快的性能会更好吗? 列表项 - 包含大约 1.000.000 条记录 CalcItem.Additional - 包含大约 5 - 10 条记录

【问题讨论】:

    标签: c# .net data-structures memory-management


    【解决方案1】:

    回答“更快”的正确方法是为您的典型数据计时。但是字典更方便(无需强制转换)和高效(无需装箱)。

    但是,如果数据键是连续的,最好只使用 List-of-double,并将键用作索引(如果您的数据没有开始,则使用偏移量在 0)。

    【讨论】:

    • 最好的!双列表 - 令人难以置信的想法 - 看看我的答案和新的 CalcItem 类!非常感谢。
    【解决方案2】:

    我认为this StackOverflow question 接受的答案也回答了你的问题。

    简而言之,两种数据结构在大多数情况下的性能非常相似。如果它对您很重要,您可以(并且应该)衡量。

    【讨论】:

      【解决方案3】:

      Marc Gravell - 与 List-of-double 的糟糕决定!!!我怎么能错过呢?! 内存减少了两倍! 这是我的新代码:

          using System;
          using System.Collections;
          using System.Collections.Generic;
      
          namespace CSharpSampleApplication.Data.CoreObjects
          {
              [Serializable]
              public class CalcItem
              {
                  public CalcItem()
                  {
                      _additional = new List<double>();
                  }
      
                  private readonly List<double> _additional;
      
                  public bool ContainsKey(int id)
                  {
                      return _additional.Count - 1 >= id;
                  }
      
                  public void Add(int id, double value)
                  {
                      if(ContainsKey(id))
                          _additional[id] = value;
                      else
                      {
                          while (!ContainsKey(id))
                          {
                              _additional.Add(0);
                          }
                          _additional[id] = value;
                      }
                  }
      
                  public DateTime Date { get; set; }
      
                  public object this[int id]
                  {
                      get
                      {
                          return _additional[id];
                      }
                  }
              }
      
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-16
        • 2011-03-26
        • 2012-06-12
        • 2010-09-29
        • 2011-03-14
        • 2012-07-15
        相关资源
        最近更新 更多