Is a string
key faster than a int
key in a Dictionary<,>
?
相关问题
- Generic Generics in Managed C++
- How to Debug/Register a Permanent WMI Event Which
- 'System.Threading.ThreadAbortException' in
- Faster loop: foreach vs some (performance of jsper
- Bulk update SQL Server C#
I know this is pretty old and answered question, so this answer for everybody who look for it in future. Also for me it was interesting question and I tried to find practical answer to this question, and result is pretty interesting.
Generally String as key is much slower than int as key.
Code:
Result:
No. First of all,
Dictionary
[UPDATED] uses hash code of the keys to find them in its internal storage - rather than the keys. And Hashcode is anint
. Forint
, it is just the value of theint
, forstring
it has to be generated.So using
int
is slightly faster.In fact generating hash code for a string is a pretty complex process (snippet using Reflector) [Hope this is not taken as copyright breach because it is NOT]: