What is the fastest dictionary-based class implementation?

Which of the C # <Key, Value> framework implementations has the best performance and faster speed?

PS1: I have two streams, one of them is written to the collection, and the other reads and writes to it.

PS2: The key elements are random numbers, and then my access is random. Writing and reading at the same time. I use hashtable, but I want to know if there is a more efficient implementation with less use of resources and better performance?

+4
source share
1 answer

There are many options for profiling. One free profiler that I used and which I would recommend is EQATEC . There are many more options, many of which are mentioned in this SO question .

As for the implementations, the first few that come to mind are Dictionary<TKey, TValue> , SortedDictionary<TKey, TValue> and SortedList<TKey, TValue> . Of course, I would be inclined to guess that Dictionary<TKey, TValue> is the fastest, since it is the simplest in terms of features. But I never tested them against each other for speed.

Note that the above classes are general, which should make them more efficient than HashTable in at least one sense: they do not require boxing / unpacking of keys and values ​​like System.Object , which leads to unnecessary memory allocation.

Something else you need to know about is that since you are using a multi-threaded script, you need to take care to somehow block your reads / writes. (The above classes, unlike HashTable , are not thread safe for multiple readers and the writer.) Locking on a shared object may be a better choice in most cases, whereas if you are doing more reads than writes, you might want to use ReaderWriterLock or ReaderWriterLockSlim (both of which allow you to switch between multiple simultaneous readers and one author). If you list a collection, you really should be blocked anyway - even with a HashTable .

+3
source

Source: https://habr.com/ru/post/1302859/


All Articles