I've been trying to build a database of entities using a Dictionary with GUID as key, I just want to know which one is faster, or takes less memory in comparison to others.
Here are the dictionaries with different Key types:
String
public Dictionary<string, Object> unityObjects;
GUID
public Dictionary<System.Guid, Object> unityObjects;
BigInteger
public Dictionary<BigInteger, Object> unityObjects;
The dictionary keys are being generated using System.Guid:
Guid guid = Guid.NewGuid();
// Using string dictionary
unityObjects.Add(guid.ToString(), myObj);
// Using GUID dictionary
unityObjects.Add(guid, myObj);
// Using BigInteger dictionary
BigInteger bigInt = new BigInteger(guid.ToByteArray());
unityObjects.Add(guid, myObj);
Or should I use a smaller unique identifier with an Int32 or Int64 instead of GUID? (Something like below code)
System.Random rng = new System.Random();
byte[] buf = new byte[8];
rng.NextBytes(buf);
long longRand = BitConverter.ToInt64(buf, 0);
// A GUID with Int64 data type
long guid = (System.Math.Abs(longRand % (long.MaxValue - 0)) + 0);