简体   繁体   English

什么是最快的基于字典的类实现?

[英]What is the fastest dictionary based class implementation?

Which one of the C# <Key, Value> structure implementations has better performance and higher speed? 哪个C# <Key, Value>结构实现具有更好的性能和更高的速度?

PS1: I Have two thread, one of them writes into collection, and another one reads and writes into it. PS1:我有两个线程,其中一个写入集合,另一个读取和写入。

PS2: Key items are random numbers, and then my access is random. PS2:关键项是随机数,然后我的访问是随机的。 Write and read actions are simultaneous. 写入和读取操作是同时进行的。 I am using hashtable, but I want to know is there any better implementation with less resource usage and better performance? 我正在使用哈希表,但我想知道是否有更好的实现,资源使用更少,性能更好?

For profiling yourself, there are many options. 对于自己的分析,有很多选择。 One free profiler I've used and which I would recommend is EQATEC . 我使用过一个免费的分析器,我推荐的是EQATEC There are plenty more to choose from, many of which are referenced in this SO question . 还有更多可供选择,其中许多都是在这个SO问题中引用的。

As for implementations, the first few that pop into mind are Dictionary<TKey, TValue> , SortedDictionary<TKey, TValue> and SortedList<TKey, TValue> . 至于实现,首先考虑的是Dictionary<TKey, TValue>SortedDictionary<TKey, TValue>SortedList<TKey, TValue> Of course, I would be inclined to guess that Dictionary<TKey, TValue> is the fastest since it's the simplest in terms of features. 当然,我倾向于猜测Dictionary<TKey, TValue>是最快的,因为它在功能方面是最简单的。 But I haven't ever tested them against one another for speed. 但我还没有为了速度而相互测试它们。

Note that the above classes are all generic, which should make them more efficient than HashTable in at least one sense: they do not require boxing/unboxing of keys and values as System.Object , which results in unnecessary memory allocation. 请注意,上面的类都是通用的,这应该使它们在至少一种意义上比HashTable更有效:它们不需要将键和值装箱/取消装箱作为System.Object ,这会导致不必要的内存分配。

Something else to be aware of is that since you're in a multithreaded scenario, you'll need to take care to lock your reads/writes somehow. 需要注意的是,由于您处于多线程场景中,因此您需要注意以某种方式锁定读/写。 (The above classes, unlike HashTable , are not guaranteed to be thread-safe for multiple readers and a writer.) Locking on a common object may be your best bet in most cases, whereas if you're performing more reads than writes you might want to consider using a ReaderWriterLock or ReaderWriterLockSlim (both of which permit switching between multiple simultaneous readers and a single writer). (与HashTable不同,上述类不保证对多个读者和编写者来说是线程安全的。)在大多数情况下,锁定公共对象可能是最好的选择,而如果您执行的读取次数多于写入次数,则可能想要考虑使用ReaderWriterLockReaderWriterLockSlim (两者都允许在多个同时读取器和单个写入器之间切换)。 If you are enumerating over the collection then you should really be locking anyway--even with a HashTable . 如果你在集合上进行枚举 ,那么你应该真正锁定 - 即使使用HashTable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以最快的方式基于字典过滤列表 - Filtering a list based on a dictionary in a fastest way 更改词典的最快方法是什么 <K,V> ? - What is the fastest way of changing Dictionary<K,V>? 根据 class 的当前实现,通过直接枚举将 ConcurrentDictionary 复制到普通字典是否安全? - Is it safe to copy a ConcurrentDictionary to a normal Dictionary, by enumerating it directly, based on the current implementation of the class? 什么是键上C#集合中像&#39;x%&#39;这样的sql最快的实现 - What is the fastest implementation of sql like 'x%' in c# collections on a key 基于接口实现的类构造 - Class construction based on interface implementation 将类转换为XML的最快方法是什么 - What is the fastest way to convert a class to XML IEquatable的类实现,用作字典中的键 - Class implementation of IEquatable for use as a key in a dictionary 根据类元素组织字典 - Organizing dictionary based on class elements 在类的属性中,Dictionary有什么问题? - What wrong with Dictionary in property of class? 转换以下字节数组的最快方法是什么 - &gt;类对象的集合 - What is the fastest way to convert the following byte array -> collection of class objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM