Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the length of key affect Dictionary performance?

I will use a Dictionary in a .NET project to store a large number of objects. Therefore I decided to use a GUID-string as a key, to ensure unique keys for each object.

Does a large key such as a GUID (or even larger ones) decrease the performance of a Dictionary, e.g. for retrieving an object via its key?

Thanks, Andrej

like image 790
Andrej Avatar asked Oct 08 '09 17:10

Andrej


1 Answers

I would recommend using an actual Guid rather than the string representation of the Guid. Yes, when comparing strings the length does affect the number of operations required, since it has to compare the strings character-by-character (at a bare minimum; this is barring any special options like IgnoreCase). The actual Guid will give you only 16 bytes to compare rather than the minimum of 32 in the string.

That being said, you are very likely not going to notice any difference...premature optimization and all that. I would simply go for the Guid key since that's what the data is.

like image 107
Adam Robinson Avatar answered Sep 28 '22 00:09

Adam Robinson