Hashmap vs Array performance

前端 未结 7 1205
伪装坚强ぢ
伪装坚强ぢ 2020-12-02 18:29

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 exam

相关标签:
7条回答
  • 2020-12-02 19:07

    Arrays will usually be faster than Collections classes.

    PS. You mentioned HashTable in your post. HashTable has even worse performance thatn HashMap. I assume your mention of HashTable was a typo

    "The HashTable one looks much much better "

    0 讨论(0)
  • 2020-12-02 19:22

    The example is strange. The key problem is whether your data is dynamic. If it is, you could not write you program that way (as in the array case). In order words, comparing between your array and hash implementation is not fair. The hash implementation works for dynamic data, but the array implementation does not.

    If you only have static data (6 fixed objects), array or hash just work as data holder. You could even define static objects.

    0 讨论(0)
  • 2020-12-02 19:23

    arrays when the indexes are know are faster (HashMap uses an array of linked lists behind the scenes which adds a bit of overhead above the array accesses not to mention the hashing operations that need to be done)

    and FYI HashMap<String,SomeObject> objects = HashMap<String,SomeObject>(); makes it so you won't have to cast

    0 讨论(0)
  • 2020-12-02 19:27

    Please, never, ever use extended if / else if / else if / else if / else if / else if cases like that. The reason I repeated it so many times is just to make you feel like your java interpreter does when it hits code-blocks like that.

    As soon as you have more than one else if, either use a hashmap, or a switch / case (java 7 will let you do it on Strings, and java 6 you have to use an enum). An even better solution for read-only checking is an ImmutableMap from a framework like guava; they have highly optimized reads as they don't allow writes.

    0 讨论(0)
  • 2020-12-02 19:29

    For the example shown, HashTable wins, I believe. The problem with the array approach is that it doesn't scale. I imagine you want to have more than two entries in the table, and the condition branch tree in doSomethingToObject will quickly get unwieldly and slow.

    0 讨论(0)
  • 2020-12-02 19:32

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

    0 讨论(0)
提交回复
热议问题