Preamble: I'm working at heavy-loaded applicaion that produces large data arrays.
I wrote the following class
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);
So the manager stores [string key]-[int index] bindings for all the calcItems.
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