HashMap的VS阵列性能 [英] Hashmap vs Array performance

查看:191
本文介绍了HashMap的VS阵列性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它是(性能明智)更好地使用数组或HashMaps这样当数组的索引知?请记住,中如'对象数组/图仅仅是一个例子,在我真正的项目它是由另一个类产生的,所以我不能用单个变量。

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:

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:

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
}

HashMap中一个看起来非常非常好,但我真的需要这个表现让优先。

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

编辑:好Array的是话,建议仍欢迎

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

编辑:我忘了提,阵列/ HashMap的大小总是相同(6)

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

编辑:看样子,HashMaps这样速度更快
阵列:128毫秒
哈希:103ms

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

在使用较少的周期HashMaps这样甚至两倍的速度

When using less cycles the HashMaps was even twice as fast

测试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();
}

}

推荐答案

HashMap中使用数组之下,因此它可以永远比正确使用数组更快。

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

Random.nextInt()比你要测试什么,甚至使用数组来测试一个数组是要偏向你的结果要慢许多倍。

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

原因阵列基准是如此缓慢是由于equals比较,而不是数组访问本身。

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

哈希表通常比HashMap的要慢得多,因为它做同样的事情,但也是同步的。

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

与微基准测试常见的问题是这是在消除code这并不做任何事情很好的JIT。如果你不小心,你只会测试你是否混淆了JIT,以至于它不能锻炼你的code没有做任何事情。

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.

这是你可以写微基准测试出其执行C ++系统的原因之一。这是因为Java是一种简单的语言,更容易推理,从而检测code这没有任何用处。这可能会导致测试这表明Java那样没什么用处比C ++更快;)

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++ ;)

这篇关于HashMap的VS阵列性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆