问题描述
Object 类型的 Array 和 Object 类型的 ArrayList 哪个性能更好?
Which one is better in performance between Array of type Object and ArrayList of type Object?
假设我们有一个 Animal
对象数组:Animal animal[]
和一个数组列表:ArrayList list
Assume we have a Array of Animal
objects : Animal animal[]
and a arraylist : ArrayList list<Animal>
现在我在做 animal[10]
和 list.get(10)
哪个应该更快,为什么?
Now I am doing animal[10]
and list.get(10)
which one should be faster and why?
推荐答案
很明显,array[10] 比 array.get(10) 快,因为后者在内部执行相同的调用,但增加了函数调用加上额外的检查.
It is pretty obvious that array[10] is faster than array.get(10), as the later internally does the same call, but adds the overhead for the function call plus additional checks.
然而,现代 JIT 会在一定程度上优化这一点,您很少需要担心这一点,除非您有一个非常关键的性能应用程序并且这已被测量为您的瓶颈.
Modern JITs however will optimize this to a degree, that you rarely have to worry about this, unless you have a very performance critical application and this has been measured to be your bottleneck.
这篇关于Array 与 ArrayList 的性能对比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!