Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hashmap vs Array performance

Is it (performance-wise) better to use Arrays or HashMaps when the indexes of the Array are known? Keep in mind that the 'objects array/map' in the example is just an example, in my real project it is generated by another class so I cant use individual variables.

ArrayExample:

SomeObject[] objects = new SomeObject[2]; objects[0] = new SomeObject("Obj1"); objects[1] = new SomeObject("Obj2");  void doSomethingToObject(String Identifier){     SomeObject object;     if(Identifier.equals("Obj1")){         object=objects[0];     }else if(){         object=objects[1];     }     //do stuff } 

HashMapExample:

HashMap objects = HashMap(); objects.put("Obj1",new SomeObject()); objects.put("Obj2",new SomeObject());  void doSomethingToObject(String Identifier){     SomeObject object = (SomeObject) objects.get(Identifier);     //do stuff } 

The HashMap one looks much much better but I really need performance on this so that has priority.

EDIT: Well Array's it is then, suggestions are still welcome

EDIT: I forgot to mention, the size of the Array/HashMap is always the same (6)

EDIT: It appears that HashMaps are faster Array: 128ms Hash: 103ms

When using less cycles the HashMaps was even twice as fast

test code:

import java.util.HashMap; import java.util.Random;  public class Optimizationsest { private static Random r = new Random();  private static HashMap<String,SomeObject> hm = new HashMap<String,SomeObject>(); private static SomeObject[] o = new SomeObject[6];  private static String[] Indentifiers = {"Obj1","Obj2","Obj3","Obj4","Obj5","Obj6"};  private static int t = 1000000;  public static void main(String[] args){     CreateHash();     CreateArray();     long loopTime = ProcessArray();     long hashTime = ProcessHash();     System.out.println("Array: " + loopTime + "ms");     System.out.println("Hash: " + hashTime + "ms"); }  public static void CreateHash(){     for(int i=0; i <= 5; i++){         hm.put("Obj"+(i+1), new SomeObject());     } }  public static void CreateArray(){     for(int i=0; i <= 5; i++){         o[i]=new SomeObject();     } }  public static long ProcessArray(){     StopWatch sw = new StopWatch();     sw.start();     for(int i = 1;i<=t;i++){         checkArray(Indentifiers[r.nextInt(6)]);     }     sw.stop();     return sw.getElapsedTime(); }    private static void checkArray(String Identifier) {     SomeObject object;     if(Identifier.equals("Obj1")){         object=o[0];     }else if(Identifier.equals("Obj2")){         object=o[1];     }else if(Identifier.equals("Obj3")){         object=o[2];     }else if(Identifier.equals("Obj4")){         object=o[3];     }else if(Identifier.equals("Obj5")){         object=o[4];     }else if(Identifier.equals("Obj6")){         object=o[5];     }else{         object = new SomeObject();     }     object.kill(); }  public static long ProcessHash(){     StopWatch sw = new StopWatch();     sw.start();     for(int i = 1;i<=t;i++){         checkHash(Indentifiers[r.nextInt(6)]);     }     sw.stop();     return sw.getElapsedTime(); }  private static void checkHash(String Identifier) {     SomeObject object = (SomeObject) hm.get(Identifier);     object.kill(); } 

}

like image 203
Timotheus Avatar asked Jun 24 '11 00:06

Timotheus


People also ask

Is a HashMap faster than an array?

According to a stackoverflow post, "HashMap uses an array underneath so it can never be faster than using an array correctly".

Is HashMap better than array?

While HashMap stores elements with key and value pairs that means two objects. So HashMap takes more memory comparatively. ArrayList allows duplicate elements while HashMap doesn't allow duplicate keys but does allow duplicate values.

Is array faster than map Java?

Arrays have a better performance than maps since you know which element you want to access, as much as maps have constant access, arrays have instant access if called by their index.

Which is faster map or ArrayList?

While the HashMap will be slower at first and take more memory, it will be faster for large values of n. The reason the ArrayList has O(n) performance is that every item must be checked for every insertion to make sure it is not already in the list.


1 Answers

HashMap uses an array underneath so it can never be faster than using an array correctly.

Random.nextInt() is many times slower than what you are testing, even using array to test an array is going to bias your results.

The reason your array benchmark is so slow is due to the equals comparisons, not the array access itself.

HashTable is usually much slower than HashMap because it does much the same thing but is also synchronized.

A common problem with micro-benchmarks is the JIT which is very good at removing code which doesn't do anything. If you are not careful you will only be testing whether you have confused the JIT enough that it cannot workout your code doesn't do anything.

This is one of the reason you can write micro-benchmarks which out perform C++ systems. This is because Java is a simpler language and easier to reason about and thus detect code which does nothing useful. This can lead to tests which show that Java does "nothing useful" much faster than C++ ;)

like image 125
Peter Lawrey Avatar answered Oct 04 '22 04:10

Peter Lawrey