简体   繁体   English

HashSet 中的哈希性能<int>针对列表<int>包含</int></int>

[英]Hashing performance in a HashSet<int> against a List<int> with Contains

I am looking for a comparison/performance considerations between a list of integers against a hash set of integers .我正在寻找整数列表与 hash 整数集之间的比较/性能注意事项 This is what What is the difference between HashSet<T> and List<T>?这就是HashSet<T> 和 List<T> 的区别是什么? talks about for T as integer.谈论T为 integer。

I will have up to several thousand integers, and I want to find out, for individual integers, whether they are contained in this set.我将有多达几千个整数,并且我想找出对于单个整数,它们是否包含在这个集合中。

Now of course this screams for a hash set, but I wonder whether hashing is beneficial here, since they are just integers to start with.现在当然这需要一个 hash 集合,但我想知道散列在这里是否有益,因为它们只是整数开始。 Would hashing them first not add unnecessary overhead here?首先散列它们不会在这里增加不必要的开销吗?

Or in other words: Is using a hash set beneficial, even for sets of integers?或者换句话说:使用 hash 集是否有益,即使对于整数集也是如此?

Hashing an integer is very cheap, as you can see in the source code of the Int32.GetHashCode method:散列 integer 非常便宜,您可以在Int32.GetHashCode方法的源代码中看到:

// The absolute value of the int contained.
public override int GetHashCode()
{
    return m_value;
}

The hash of the number is the number itself.号码的 hash 就是号码本身。 It can't get any cheaper than that.没有比这更便宜的了。 So there is no reason to be concerned about the overhead.所以没有理由担心开销。 Put your numbers in a HashSet , and enjoy searching with O(1) computational complexity.将您的数字放入HashSet中,享受O(1)计算复杂度的搜索。

What ever T is there is a simple but efficient rule of thumb:无论 T 是什么,都有一个简单但有效的经验法则:

  • The collection is mainly used for adding and iterating with very few search => Use List该集合主要用于添加和迭代很少搜索 => 使用列表

  • The collection is heavely used for research => Use HashSet该集合大量用于研究 => 使用 HashSet

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM