views:

362

answers:

5

Hi. I wanted to know is C# array has a constant access speed?
I need to store 1000 items in static array, that will be initialized during server startup. This array will be used readonly, so there will be no changes to array.
Should I use a simple C# array (new MyClass[]) or Dictionary instead.

I am really new to C# and trying to understand how C# arrays access works.
Can they be compared to c++ arrays by speed?

+1  A: 

An array access in C# is a simple index operation, whereas a dictionary is a hash table lookup. Arrays are comparable to C++ arrays except for some small overhead of bounds-checking performed by the language.

If you're not going to be changing the contents, I'd use an array for data size, if nothing else.

Billy ONeal
Perhaps you mean "An array access in C# is a constant time operation"?
zneak
@zneak: Yes, thank you.
Billy ONeal
+1  A: 

Yes, if you know the index the speed is constant O(1), much like a lookup in a hashtable backed dictionary (e.g. Dictionary<>).

If the index is not known then you will have to perform a search (linear if the items are unsorted O(n) or binary if they are O(log n)).

That said, in real terms the array lookup will be quicker because a hashtable lookup is two operations: calculate the hash of the key to get an index and retrieve the value from an internal array at that index.

Also note that a if the hashcode of the key is badly implemented, the hashtable's magical properties quickly evaporate and, in the worst case (where every key has the same hashcode) you will end up with an elaborate linked-list in which every look up will be a linear search at a cost of O(n). Double check those hashcodes!

Paul Ruane
For 1000 items the logarithmic search is going to be faster than the hash table.
Billy ONeal
@BillyONeal: I don't think that's accurate. The dictionary implementation, with a property GetHashCode on key, is VERY fast - it's better than O(log n) in most cases...
Reed Copsey
Hashtable look-ups are constant time providing the hash-code has been implemented well (good distribution across the Int32 range). The multiplier (the constant) will vary depending on the hash-code complexity but the access is still constant, e.g. If I were to add Thread.Sleep(1000) in the GetHashCode() the hashtable will be slow but still constant irrespective of number of elements.
Paul Ruane
@Paul: Yes, but the implementation must not produce collisions for Dictionary to perform well. The hash code generation speed wasn't what I was referring to, but rather it's ability to produce unique hash values for unique instance values.
Reed Copsey
@Reed, yes, of course. If you use a Dictionary ensure the hashcodes are well distributed or pay the price. Obviously it is impossible to prevent collisions but minimizing them is imperitive when using hashtables.
Paul Ruane
+1  A: 

It depends on the way you are going to get elements from the array. If you are going to get elements by positions (index) in the array then array will be quicker (or at least not slower than dictionary). If you are going to search for elements in the array than dictionary will be faster.

Andrew Bezzub
+2  A: 

c# are slightly slower because they check array bounds, c++ array doesn't. but it is not too big overhead. if you want to reference your collection by sequential int key then use arrays of course. if you need non sequential int key, like string your only option is dictionary.

Andrey
+10  A: 

The best choice depends on how you need to access the elements.

If you want to access them by index, then use an Array. Arrays in C# have constant access speed, and are very similar to a C++ array in terms of speed for access.

Dictionaries, however, have very fast access (the Item property approaches O(1) access time, but depends on how good of an implementation the stored key has for GetHashCode). If you need to lookup your items based on a key value and not by index, then dictionary would be appropriate instead.

Reed Copsey