Java Array 与 ArrayList 的性能对比
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19389609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Array vs ArrayList in performance
提问by Spark-Beginner
Which one is better in performance between Array of type Object and ArrayList of type Object?
Object 类型的 Array 和 Object 类型的 ArrayList 哪个性能更好?
Assume we have a Array of Animal
objects : Animal animal[]
and a arraylist : ArrayList list<Animal>
假设我们有一个Animal
对象数组:Animal animal[]
和一个数组列表:ArrayList list<Animal>
Now I am doing animal[10]
and list.get(10)
which one should be faster and why?
现在我在做什么 animal[10]
, list.get(10)
哪个应该更快,为什么?
采纳答案by TwoThe
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.
很明显,array[10] 比 array.get(10) 快,因为后者在内部执行相同的调用,但增加了函数调用的开销和额外的检查。
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.
然而,现代 JIT 会在一定程度上优化这一点,您很少需要担心这一点,除非您有一个非常关键的性能应用程序,并且这已被测量为您的瓶颈。
回答by Ankit Rustagi
Arrays are better in performance. ArrayList provides additional functionality such as "remove" at the cost of performance.
数组的性能更好。ArrayList 以牺牲性能为代价提供了额外的功能,例如“删除”。
回答by Rahul Tripathi
From here:
从这里:
ArrayList is internally backed by Array in Java, any resize operation in ArrayList will slow down performance as it involves creating new Array and copying content from old array to new array.
ArrayList 由 Java 中的 Array 内部支持,ArrayList 中的任何调整大小操作都会降低性能,因为它涉及创建新 Array 并将内容从旧数组复制到新数组。
In terms of performance Array and ArrayList provides similar performance in terms of constant time for adding or getting element if you know index. Though automatic resize of ArrayList may slow down insertion a bit Both Array and ArrayList is core concept of Java and any serious Java programmer must be familiar with these differences between Array and ArrayList or in more general Array vs List.
在性能方面 Array 和 ArrayList 如果您知道 index ,则在添加或获取元素的恒定时间方面提供类似的性能。尽管 ArrayList 的自动调整大小可能会稍微减慢插入速度 Array 和 ArrayList 都是 Java 的核心概念,任何认真的 Java 程序员都必须熟悉 Array 和 ArrayList 之间的这些差异,或者更一般的 Array 与 List。
回答by Paul Samsotha
When deciding to use Array or ArrayList, your first instinct really shouldn't be worrying about performance, though they do perform differently. You first concern should be whether or not you know the size of the Array before hand. If you don't, naturally you would go with an array list, just for functionality.
在决定使用 Array 或 ArrayList 时,您的第一直觉真的不应该担心性能,尽管它们的性能有所不同。您首先应该关心的是您是否事先知道 Array 的大小。如果你不这样做,你自然会使用一个数组列表,只是为了功能。
回答by user949300
I agree with somebody's recently deleted post that the differences in performance are so small that, with very very few exceptions, (he got dinged for saying never) you should not make your design decision based upon that.
我同意某人最近删除的帖子,即性能差异如此之小,除了极少数例外,(他因说从不而受到谴责)您不应该基于此做出设计决定。
In your example, where the elements are Objects, the performance difference should be minimal.
在您的示例中,元素是对象,性能差异应该很小。
If you are dealing with a largenumber of primitives, an array will offer significantly better performance, both in memory and time.
如果您正在处理大量的原语,数组将在内存和时间方面提供明显更好的性能。