【问题标题】:Which is faster, String or Integer as hashkey in Java?哪个更快,String 或 Integer 作为 Java 中的 hashkey?
【发布时间】:2013-03-26 08:17:50
【问题描述】:

我正在解决一个问题,我遇到了执行时间变得过长的问题,现在我正在寻找可能的优化。

问题:使用 String 或 Integer 作为 haskey 在性能上是否有任何(相当大的)差异?

问题是我有一个图,其中节点存储在哈希表中,字符串作为键。例如,键如下 - “0011”或“1011”等。现在我也可以将它们转换为整数,如果这意味着执行时间的改进。

【问题讨论】:

  • String 的哈希码是缓存的,所以应该不会有太大的区别。问题可能出在其他地方......您应该分析您的代码以找到瓶颈。注意:在单线程情况下,HashMap 的性能会比 Hashtable 稍好。
  • 我建议你分析你的应用程序,看看它什么时候花费了大部分时间。以这种方式更改关键时间不太可能产生影响。

标签: java hashtable


【解决方案1】:

Integer 将比 String 执行得更好。以下是两者的哈希码计算代码。

整数哈希码实现

/**
     * Returns a hash code for this <code>Integer</code>.
     *
     * @return  a hash code value for this object, equal to the 
     *          primitive <code>int</code> value represented by this 
     *          <code>Integer</code> object. 
     */
    public int hashCode() {
    return value;
    }

字符串哈希码实现

 /**
     * Returns a hash code for this string. The hash code for a
     * <code>String</code> object is computed as
     * <blockquote><pre>
     * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
     * </pre></blockquote>
     * using <code>int</code> arithmetic, where <code>s[i]</code> is the
     * <i>i</i>th character of the string, <code>n</code> is the length of
     * the string, and <code>^</code> indicates exponentiation.
     * (The hash value of the empty string is zero.)
     *
     * @return  a hash code value for this object.
     */
    public int hashCode() {
    int h = hash;
    if (h == 0) {
        int off = offset;
        char val[] = value;
        int len = count;

            for (int i = 0; i < len; i++) {
                h = 31*h + val[off++];
            }
            hash = h;
        }
        return h;
    }

【讨论】:

  • 这表明除了第一次计算之外,性能是相似的。
【解决方案2】:

如果您遇到性能问题,则该问题不太可能是由 HashMap/HashTable 引起的。虽然散列字符串比散列整数稍微贵一些,但差别很小,而且 hashCode 被缓存,因此如果您使用相同的字符串对象不会重新计算它,您不太可能从首先将其转换为整数获得任何显着的性能优势。

在其他地方寻找性能问题的根源可能更有成效。您是否尝试过分析您的代码?

【讨论】:

    【解决方案3】:

    速度有差异。 HashMaps会使用hashCode根据该代码计算bucket,Integer的实现比String简单很多。

    话虽如此,如果您在执行时间方面遇到问题,则需要进行一些适当的测量并分析自己。这是找出执行时间问题的唯一方法,使用整数而不是字符串通常对性能的影响很小,这意味着您的性能问题可能在其他地方。

    例如,如果您想做一些适当的微基准测试,请查看this post。还有许多其他资源可用于分析等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-05
      • 1970-01-01
      • 2010-10-23
      • 1970-01-01
      • 2012-05-26
      • 1970-01-01
      相关资源
      最近更新 更多