【问题标题】:Hash by Chaining VS Double Probing链式哈希 VS 双重探测
【发布时间】:2012-12-12 11:22:47
【问题描述】:

我正在尝试比较链接和双重探测。 我需要在表大小 100 中插入 40 个整数, 当我用纳米时间测量时间时(在java中) 我知道Double更快。 那是因为在链接的插入方法中,我每次都创建 LinkedListEntry, 这是添加时间。 Chaining 怎么会比 Double Probing 更快呢? (这是我在维基百科上看到的)

谢谢!!

这是链接的代码:

public class LastChain
{
    int tableSize;
     Node[] st;
    LastChain(int size) {
        tableSize = size;
        st = new Node[tableSize];
        for (int i = 0; i < tableSize; i++)
            st[i] = null;
    }

    private class Node
    {
        int key;
        Node next;
        Node(int key, Node next)
        {
            this.key   = key;
            this.next  = next;
        }
    }

    public void put(Integer key) 
    {
       int i = hash(key);
       Node first=st[i];
       for (Node x = st[i]; x != null; x = x.next)
          if (key.equals(x.key))
             { 
             return; 
              }

       st[i] = new Node(key, first);

    }


    private int hash(int key)
    {  return key%tableSize;
    }

      }
}

这是双重探测的相关代码:

public class HashDouble1 {
  private Integer[] hashArray; 

  private int arraySize;

  private Integer bufItem; // for deleted items

  HashDouble1(int size) {
    arraySize = size;
    hashArray = new Integer[arraySize];
    bufItem = new Integer(-1);
  }



  public int hashFunc1(int key) {
    return key % arraySize;
  }

  public int hashFunc2(int key) {
    return 7 - key % 7;
  }

  public void insert(Integer key) {
        int hashVal = hashFunc1(key); // hash the key
        int stepSize = hashFunc2(key); // get step size
        // until empty cell or -1
        while (hashArray[hashVal] != null && hashArray[hashVal] != -1) {
          hashVal += stepSize; // add the step
          hashVal %= arraySize; // for wraparound
        }
        hashArray[hashVal]  = key; // insert item
      }





}

这样,Double 中的 insert 比 Chaining 更快。 我该如何解决?

【问题讨论】:

  • 双重探测......所有那些可怜的奶牛。
  • 写一个由特定代码支持的特定问题,你离得到认真关注又近了一步。

标签: java data-structures complexity-theory double-hashing


【解决方案1】:

链式在负载系数较高的情况下效果最佳。尝试在 100 个表中使用 90 个字符串(不是很好的整数选择)。

链式也更容易实现删除/删除。

注意:在 HashMap 中,Entry 对象无论是否被链接都会被创建,并不是没有保存。

【讨论】:

    【解决方案2】:

    Java 有一个特殊的“特性”对象会占用大量内存。

    因此,对于大型数据集(这将具有任何相关性)双重探测将是好的。

    但首先,请将您的 Integer[] 更改为 int[] -> 内存使用量将是四分之一左右,性能会大幅提升。

    但总是有性能问题:测量、测量、测量,因为您的情况总是很特别。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-16
      • 1970-01-01
      • 2013-03-25
      • 1970-01-01
      • 2021-02-13
      • 1970-01-01
      • 2022-07-17
      • 2014-03-24
      相关资源
      最近更新 更多