[英]Dictionary of integers or references key - performance?
I am interested to know the difference in Dictionary
read and write performance between using an int
value type key and an integer wrapped into a reference type ( IntId
). 我很想知道使用int
值类型键和包装为引用类型( IntId
)的整数之间在Dictionary
读取和写入性能方面的区别。
Instantiating with int
: 用int
实例化:
new Dictionary<int, SomeValue>();
Instantiating with IntId
: 用IntId
实例化:
new Dictionary<IntId, SomeValue>();
Averaged performance results yielded the following on writing and reading 200,000 items: 平均性能结果在写入和读取200,000个项目时产生以下结果:
IntId
is 2X faster at writing IntId
写入速度快2倍 int
is 3X faster at reading int
读取速度快3倍 What is the discrepancy between these two results? 这两个结果之间有何差异?
The IntId
class: IntId
类:
public class IntId
{
public int Value { get; private set; }
public IntId(int value)
{
Value = value;
}
public override bool Equals(object other)
{
var entity = other as IntId;
if (entity == null)
{
return false;
}
else
{
return (this.Value.Equals(entity.Value));
}
}
public override int GetHashCode()
{
return Value.GetHashCode();
}
}
In all likelihood the discrepancy between these results will be strongly related to the method used to obtain them. 这些结果之间的差异极有可能与获得它们的方法密切相关。
Your IntId is a small reference object. 您的IntId是一个小的参考对象。 Creating one requires a memory allocation. 创建一个需要分配内存。
An integer is a value object, but may require boxing to use in your collection. 整数是一个值对象,但可能需要装箱才能在集合中使用。
The values assigned to the keys affect the efficiency of the hashing function. 分配给键的值会影响哈希函数的效率。
There are many factors to consider. 有许多因素需要考虑。 I would not accept the results on face value until after a careful scrutiny of how they were obtained. 我将不接受关于面值的结果,除非仔细检查它们的获得方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.