什么是更快更方便:Hashtable或Dictionary <int,double =“”>()?</int,>

时间:2010-10-23 07:54:02

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

序言:我正在处理生成大型数据阵列的繁重应用程序。

我写了下面的课程

    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<int, double>而不是Hashtable()来优化内存使用和更快的性能是否更好? 列表项 - 包含约1.000.000条记录 CalcItem.Additional - 包含大约5-10条记录

3 个答案:

答案 0 :(得分:5)

回答“更快”的愚蠢方法是为您的典型数据计时。然而,字典更方便(无需强制转换)和高效(无拳击)。

如果数据键是连续,那么最好只使用List-of-double,并使用键作为索引(如果数据无法启动,则使用偏移量)在0)。

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

马克·格拉维尔(Marc Gravell) - 双重榜单的绝佳决定!我怎么能错过这个?! 记忆减少了两次! 这是我的新代码:

    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];
                }
            }
        }


    }
相关问题