I have got tons of objects, which have two Int32
Keys to identify them in a collection. The question is the choice of collection. The only criteria is pure performance for accessing/"reading" an object(no need to be writable).
Stuff I analyzed:
I didn't try hashtables cause of this. I had got the idea to try around with following types:
Dictionary<Tuple<int,int>,object>
Dictionary<long,object> // putting the bytes of two ints together by first*(int.MaxValue+1L)+second
Dictionary<int, List<Tuple<int,object>>
Dictionary<int, Dictionary<int,object>>
List<Tuple<int,int,object>>
List<Tuple<long,object>>
List<Tuple<int,Tuple<int,object>>
I filled them with 1000 values for the first and 100 values for the second integer. The "object" for testing was firstInt.ToString()+secondInt.ToString()
I measured using following pattern:
System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch();
bool b=true;
st.Start();
for(int i=0;i<100;i++)
{
for (int ii = 0; ii< 1000; ii++)
{ //for making sure code optimization doesn't jump over it
b=b&*accessor*.Contains('a');
}
}
st.Stop();
Console.WriteLine("Result: "+st.ElapsedMilliseconds);
I used following accessors:
Dictionary<Tuple<int,int>,object> dir [...]
Accessor: dir[a][b]
Result: 211ms
Dictionary<long,object> dir [...]
Accessor: dir[merge(a,b)]
Result: 70ms
//method for sticking two ints together
public static long merge(int a,int b)
{ return (int.MaxValue+1L)*a+b;}
Dictionary<int, List<Tuple<int,object>> dir[...]
Accessor: dir[a].Find(x=>x.Item1==b).Item2
Result: 15ms
Dictionary<int, Dictionary<int,object>> dir[...]
Accessor: dir[a][b]
Result: 22ms
List<Tuple<int,int,object>> dir[...]
Accessor: dir.Find(x=>x.Item1==a&&x.Item2==b).Item3
Result: over60s
List<Tuple<long,object>> dir[...]
Accessor: dir.Find(x=>x.Item1==merge(a,b)).Item2
Result: over60s
List<Tuple<int,Tuple<int,object>> dir[...]
Accessor: dir.Find(x=>x.Item==a).Item2.Find(x=>x.Item==b).Item2
Result: over60s
Are there better/faster types? any recommendations about improving the performance? or is there a quite different solution state-of-the-art?