gpt4 book ai didi

c# - 如何降低 GetHashCode 的复杂性

转载 作者:行者123 更新时间:2023-12-02 05:10:48 24 4
gpt4 key购买 nike

我想减少我的代码执行时间。查看一些测试结果,我发现 GetHashCode() 占用了我 21.62% 的执行时间。

我也收到警告:

Warning 1 DA0010: .*.GetHashCode() = 7,63; GetHashCode functions should be cheap and not allocate any memory. Reduce complexity of hash code function if possible.

代码片段:

字段类中的我的 GetHashCode():

    public override int GetHashCode()
{
int hash = 7;
hash = (hash * 13) + this.Coordinate.GetHashCode();
return hash;
}

坐标类中的我的 GetHashCode():

    public override int GetHashCode()
{
int hash = 17;

hash = (hash * 23) + this.Row.GetHashCode();
hash = (hash * 23) + this.Column.GetHashCode();

return hash;
}

编辑: Row 和 Column 只是字节变量。我只是调用他们的属性,它在 get 访问器中返回一个字节

我在数独类中的 GetHashCode():

    public override int GetHashCode()
{
int hash = 7;

hash = (hash * 5) + this.Grid.GetHashCode();

return hash;
}

编辑: Grid 只是一个多维数组,类型为:Field[,],我只是在这里称它为 Property,它通过它返回一个 Field[,] 网格访问器。

问题:如何大大降低 GetHashCode() 的复杂性并提高其性能?为什么 GetHashCode() 方法的性能这么低?

最佳答案

我怀疑您会发现 GetHashCode 不是您的问题。如果您在 GetHashCode 上花费了超过 20% 的时间,那么您必须进行大量的字典查找。或者您将哈希码用于您可能不应该将其用于的用途。

GetHashCode 可能是性能问题的表现,但几乎可以肯定不是原因。

关于c# - 如何降低 GetHashCode 的复杂性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15500865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com