1

I would like to know what makes the difference, what should i aware of when im writing code.

  • Used the same parameters and methods put(), get() when testing without printing
  • Used System.NanoTime() to test runtime
  • I tried it with 1-10 int keys with 10 values, so every single hash returns unique index, which is the most optimal scenario
  • My HashSet implementation which is based on this is almost as fast as the JDK's

Here's my simple implementation:


public MyHashMap(int s) {

    this.TABLE_SIZE=s;
    table = new HashEntry[s];

}

class HashEntry {

    int key;
    String value;

    public HashEntry(int k, String v) {
        this.key=k;
        this.value=v;
    }

    public int getKey() {
        return key;
    }

}



int TABLE_SIZE;

HashEntry[] table;

public void put(int key, String value) {

    int hash = key % TABLE_SIZE;

    while(table[hash] != null && table[hash].getKey() != key)
        hash = (hash +1) % TABLE_SIZE;

        table[hash] = new HashEntry(key, value);
}


public String get(int key) {

    int hash = key % TABLE_SIZE;

        while(table[hash] != null && table[hash].key != key)
            hash = (hash+1) % TABLE_SIZE;

            if(table[hash] == null)
                return null;
            else
                return table[hash].value;

}

Here's the benchmark:

public static void main(String[] args) {


    long start = System.nanoTime();

    MyHashMap map = new MyHashMap(11);

    map.put(1,"A");
    map.put(2,"B");
    map.put(3,"C");
    map.put(4,"D");
    map.put(5,"E");
    map.put(6,"F");
    map.put(7,"G");
    map.put(8,"H");
    map.put(9,"I");
    map.put(10,"J");


    map.get(1);
    map.get(2);
    map.get(3);
    map.get(4);
    map.get(5);
    map.get(6);
    map.get(7);
    map.get(8);
    map.get(9);
    map.get(10);

    long end = System.nanoTime();

    System.out.println(end-start+" ns");


}
freestar
  • 418
  • 3
  • 18
  • 1
    The question is incomplete without a test showing the other side of the comparison, i.e. your use of `HashMap`. You should also show your micro-benchmark, because small errors in these are notorious for skewing the results completely. – Sergey Kalinichenko Oct 19 '15 at 11:57

2 Answers2

4

If you read the documentation of the HashMap class, you see that it implements a hash table implementation based on the hashCode of the keys. This is dramatically more efficient than a brute-force search if the map contains a non-trivial number of entries, assuming reasonable key distribution amongst the "buckets" that it sorts the entries into.

That said, benchmarking the JVM is non-trivial and easy to get wrong, if you're seeing big differences with small numbers of entries, it could easily be a benchmarking error rather than the code.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • `hashCode` is irrelevant here because the OP is attempting to make a map with `int` keys. – Paul Boddington Oct 19 '15 at 11:53
  • @PaulBoddington: `hashCode` is relevant to the sentence I used it in: *"...it [HashMap] implements a hash table based on the `hashCode` of the keys."* I'm not talking about the OP's code there. – T.J. Crowder Oct 19 '15 at 11:54
  • I rewrote the code,the following way: instead of using HashEntry type i used now 2 arrays with the same length: `int[] keys` and `String[] values` and the execution became faster then the JDKs. Now i got confused. @T.J.Crowder – freestar Oct 19 '15 at 13:21
0

When it is up to performance, never assume something.

Your assumption was "My HashSet implementation which is based on this is almost as fast as the JDK's". No, obviously it is not.

That is the tricky part when doing performance work: doubt everything unless you have measured with great accuracy. Worse, you even measured, and the measurement told you that your implementation is slower; and instead of checking your source, and the source of the thing you are measuring against; you decided that the measuring process must be wrong ...

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I know my code's poor, and i would like to improve my skills. – freestar Oct 19 '15 at 12:11
  • I didn't say that. I just wanted to make you aware that "working on assumptions" is dangerous; and that one really has to let that sync in and question the his own mindset constantly. – GhostCat Oct 19 '15 at 13:30