什么是更快,更方便:哈希表或字典< INT,双>()?更快、更方便、字典、INT

2023-09-06 23:14:28 作者:\\. 听歌的人梦游过

preamble:我在产生大量数据阵列重加载applicaion工作。

Preamble: I'm working at heavy-loaded applicaion that produces large data arrays.

我写了下面的类

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


    }

随后,在另一个类,我做管理器,它包含以下内容:

Then, in another class, i made manager which contains the following:

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

通过使用这些类我登录了一些实时数据,比如像这样的:

By using those classes i log some realtime data, for example like this:

                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);

所以,管理者店[字符串键] - [INT指数]绑定的所有calcItems

So the manager stores [string key]-[int index] bindings for all the calcItems.

现在的问题是: 是更好地使用词典&LT; INT,双&GT; 而不是散列表(),以优化内存使用和更快的性能比较? 清单项目 - 包含1.000.000记录 CalcItem.Additional - 含有约5 - 10条

The question is: Is it better to use Dictionary<int, double> instead of Hashtable() to optimize memory usage and faster perfomance? List Items - contains about 1.000.000 records CalcItem.Additional - contains about 5 - 10 records

推荐答案

在obly办法回答更快对时间它的您典型的数据。然而,词典更方便(无需投)和高效(无拳)。

The obly way to answer "faster" is to time it for your typical data. However the dictionary is more convenient (no need to cast) and efficient (no boxing).

如果数据密钥的连续的,不过,这将是最好只使用一个列表,中双,并使用密钥作为指数(随偏移距,如果你的数据不启动在0)。

If the data keys are contiguous, however, it would be better just to use a List-of-double, and use the key as the index (with offset if your data doesn't start at 0).