阵列比arraylist更快吗? [重复]

时间:2021-11-25 15:42:05

This question already has an answer here:

这个问题在这里已有答案:

My intuition says arrays are faster than arraylist because arraylists are implemented using arrays that resize as it fills up/loses elements.

我的直觉说阵列比arraylist更快,因为arraylists是使用在填充/丢失元素时调整大小的数组来实现的。

I just wanted to confirm if this is true or not, implying there's never a reason to use an arraylist if you know the number of elements you want to hold.

我只是想确认这是否属实,这意味着如果您知道要保留的元素数量,就没有理由使用arraylist。

8 个解决方案

#1


24  

Any performance difference will be negligible, especially if you initialize your arraylist with an initialCapacity. Write your code in whatever way makes it the most readable and maintainable, and try not to optimize stuff like this unless you've determined through testing that you are getting a significant performance hit from it.

任何性能差异都可以忽略不计,特别是如果使用initialCapacity初始化arraylist。以任何方式编写代码使其最易读和可维护,并尽量不优化这样的东西,除非你通过测试确定你从中获得了显着的性能影响。

Potential reasons to use ArrayList:

使用ArrayList的潜在原因:

  • useful methods (contains, etc.)
  • 有用的方法(包含等)
  • implements Iterable, Collection, and List, and can therefore be used in a lot of API calls that are interface-based.
  • 实现Iterable,Collection和List,因此可以用于许多基于接口的API调用。
  • even though you currently "know" that your collection's size can never change, life throws unexpected requirements at us. Why lock yourself into a pattern when there's no discernible advantage to be gained?
  • 即使你现在“知道”你的收藏品的大小永远不会改变,但生活却给我们带来了意想不到的要求。为什么在没有明显优势的情况下锁定自己的模式?

#2


5  

ArrayList gives you many features a raw array does not have. If you know the number of elements you can create an ArrayList of that size.

ArrayList为您提供了原始数组所没有的许多功能。如果您知道元素的数量,则可以创建该大小的ArrayList。

new ArrayList<String>(100);

If you are worrying about the difference in speed between an ArrayList and an array, you are worrying about the wrong thing. It is highly unlikely to be the bottleneck in your code. If it is, there is almost certainly a better answer than changing to an array.

如果你担心ArrayList和数组之间的速度差异,你会担心错误的事情。它不太可能成为您代码中的瓶颈。如果是的话,几乎可以肯定比更换阵列更好。

Don't succumb to premature optimization. It'll wreak havoc on your code. Most things don't matter, only a few things do. You can only find those few things by profiling your code. Trying to make every part fast is a very ineffective way of making the whole fast. Keeping a clean, simple design is much more effective. That will give you the necessary seams for introducing optimizations in the one or two places they're actually needed.

不要屈服于过早的优化。它会对你的代码造成严重破坏。大多数事情并不重要,只有少数事情可做。您只能通过分析代码来找到这些内容。试图让每个部分都快速完成是一种非常无效的方法,可以使整个部分变得快速。保持干净,简单的设计更加有效。这将为您提供必要的接缝,以便在实际需要的一两个地方引入优化。

#3


2  

ArrayList can't be faster at simple get/set, but the difference would be very small and likely not worth worrying about in almost any realistic scenario.

在简单的get / set中,ArrayList不能更快,但差异非常小,几乎在任何实际情况下都不值得担心。

The List API has some methods you may need, even in the case you already know the size will be fixed. Think contains() for example. You'd also have to use ArrayList in a case where you wanted an Iterator or an API needed a List -- again even if you know the list is of fixed size.

List API有一些您可能需要的方法,即使在您已经知道大小将被修复的情况下也是如此。例如,想想contains()。如果您希望Iterator或API需要List,您还必须使用ArrayList - 即使您知道列表具有固定大小也是如此。

#4


2  

As other people have said already, use ArrayList unless performance benchmarks say it is a bottle beck.

正如其他人已经说过的那样,使用ArrayList,除非性能基准测试表明它是一个瓶子。

Yes there is a performance difference due to accessor overhead, which will not be significant in general. An exception to that general rule is if you are storing primitive types inside your ArrayList. That will encore a significant performance hit as it converts the objects to/from Object and primitive types.

是的,由于访问者开销而存在性能差异,这通常不会很重要。该一般规则的一个例外是,如果要在ArrayList中存储基本类型。当它将对象转换为Object或原始类型时,这将会产生重大的性能损失。

// easier to work with but slow
ArrayList<Double> slowList;

// faster primitive data array
double[] fasterList;

#5


1  

Yes, arrays are much faster. You can see a noticeable speed-up if you running numerical algorithms.

是的,数组更快。如果运行数值算法,您可以看到明显的加速。

However for normal application purposes, you don't have to worry about it. This is because most of the runtime is spent doing other things anyways.

但是,出于正常的应用目的,您不必担心它。这是因为大多数运行时都花在做其他事情上。

#6


0  

if you know the size of the array, ensuring that it wont expand or shrink. than use arrays. if you dont know, array list are preferable and stable. for performance, is this your only concern left ? move along..

如果您知道阵列的大小,确保它不会扩展或缩小。比使用数组。如果你不知道,数组列表是优选和稳定的。对于表现,这是你唯一关心的问题吗?向前走..

#7


0  

You shouldn't optimize your code too early, instead use the data structure which fits in your code best. as you said a rule of thumb could be:

您不应过早优化代码,而应使用最适合您代码的数据结构。正如你所说,经验法则可能是:

  1. i don't know the number of elements or it is changing a lot => List (don't fixate on implementations)

    我不知道元素的数量或它正在改变很多=>列表(不要注意实现)

  2. the number of elements is fixed an known beforehand => array

    元素的数量是固定的,事先已知=>数组

#8


-2  

Arrays much faster than array list. The difference can be hundreds of percents and probably come from the JVM optimization. If you declare your array variables as volatile than you will get similar performance.

数组比数组列表快得多。差异可能是几百个百分点,可能来自JVM优化。如果将数组变量声明为volatile,则会获得类似的性能。

#1


24  

Any performance difference will be negligible, especially if you initialize your arraylist with an initialCapacity. Write your code in whatever way makes it the most readable and maintainable, and try not to optimize stuff like this unless you've determined through testing that you are getting a significant performance hit from it.

任何性能差异都可以忽略不计,特别是如果使用initialCapacity初始化arraylist。以任何方式编写代码使其最易读和可维护,并尽量不优化这样的东西,除非你通过测试确定你从中获得了显着的性能影响。

Potential reasons to use ArrayList:

使用ArrayList的潜在原因:

  • useful methods (contains, etc.)
  • 有用的方法(包含等)
  • implements Iterable, Collection, and List, and can therefore be used in a lot of API calls that are interface-based.
  • 实现Iterable,Collection和List,因此可以用于许多基于接口的API调用。
  • even though you currently "know" that your collection's size can never change, life throws unexpected requirements at us. Why lock yourself into a pattern when there's no discernible advantage to be gained?
  • 即使你现在“知道”你的收藏品的大小永远不会改变,但生活却给我们带来了意想不到的要求。为什么在没有明显优势的情况下锁定自己的模式?

#2


5  

ArrayList gives you many features a raw array does not have. If you know the number of elements you can create an ArrayList of that size.

ArrayList为您提供了原始数组所没有的许多功能。如果您知道元素的数量,则可以创建该大小的ArrayList。

new ArrayList<String>(100);

If you are worrying about the difference in speed between an ArrayList and an array, you are worrying about the wrong thing. It is highly unlikely to be the bottleneck in your code. If it is, there is almost certainly a better answer than changing to an array.

如果你担心ArrayList和数组之间的速度差异,你会担心错误的事情。它不太可能成为您代码中的瓶颈。如果是的话,几乎可以肯定比更换阵列更好。

Don't succumb to premature optimization. It'll wreak havoc on your code. Most things don't matter, only a few things do. You can only find those few things by profiling your code. Trying to make every part fast is a very ineffective way of making the whole fast. Keeping a clean, simple design is much more effective. That will give you the necessary seams for introducing optimizations in the one or two places they're actually needed.

不要屈服于过早的优化。它会对你的代码造成严重破坏。大多数事情并不重要,只有少数事情可做。您只能通过分析代码来找到这些内容。试图让每个部分都快速完成是一种非常无效的方法,可以使整个部分变得快速。保持干净,简单的设计更加有效。这将为您提供必要的接缝,以便在实际需要的一两个地方引入优化。

#3


2  

ArrayList can't be faster at simple get/set, but the difference would be very small and likely not worth worrying about in almost any realistic scenario.

在简单的get / set中,ArrayList不能更快,但差异非常小,几乎在任何实际情况下都不值得担心。

The List API has some methods you may need, even in the case you already know the size will be fixed. Think contains() for example. You'd also have to use ArrayList in a case where you wanted an Iterator or an API needed a List -- again even if you know the list is of fixed size.

List API有一些您可能需要的方法,即使在您已经知道大小将被修复的情况下也是如此。例如,想想contains()。如果您希望Iterator或API需要List,您还必须使用ArrayList - 即使您知道列表具有固定大小也是如此。

#4


2  

As other people have said already, use ArrayList unless performance benchmarks say it is a bottle beck.

正如其他人已经说过的那样,使用ArrayList,除非性能基准测试表明它是一个瓶子。

Yes there is a performance difference due to accessor overhead, which will not be significant in general. An exception to that general rule is if you are storing primitive types inside your ArrayList. That will encore a significant performance hit as it converts the objects to/from Object and primitive types.

是的,由于访问者开销而存在性能差异,这通常不会很重要。该一般规则的一个例外是,如果要在ArrayList中存储基本类型。当它将对象转换为Object或原始类型时,这将会产生重大的性能损失。

// easier to work with but slow
ArrayList<Double> slowList;

// faster primitive data array
double[] fasterList;

#5


1  

Yes, arrays are much faster. You can see a noticeable speed-up if you running numerical algorithms.

是的,数组更快。如果运行数值算法,您可以看到明显的加速。

However for normal application purposes, you don't have to worry about it. This is because most of the runtime is spent doing other things anyways.

但是,出于正常的应用目的,您不必担心它。这是因为大多数运行时都花在做其他事情上。

#6


0  

if you know the size of the array, ensuring that it wont expand or shrink. than use arrays. if you dont know, array list are preferable and stable. for performance, is this your only concern left ? move along..

如果您知道阵列的大小,确保它不会扩展或缩小。比使用数组。如果你不知道,数组列表是优选和稳定的。对于表现,这是你唯一关心的问题吗?向前走..

#7


0  

You shouldn't optimize your code too early, instead use the data structure which fits in your code best. as you said a rule of thumb could be:

您不应过早优化代码,而应使用最适合您代码的数据结构。正如你所说,经验法则可能是:

  1. i don't know the number of elements or it is changing a lot => List (don't fixate on implementations)

    我不知道元素的数量或它正在改变很多=>列表(不要注意实现)

  2. the number of elements is fixed an known beforehand => array

    元素的数量是固定的,事先已知=>数组

#8


-2  

Arrays much faster than array list. The difference can be hundreds of percents and probably come from the JVM optimization. If you declare your array variables as volatile than you will get similar performance.

数组比数组列表快得多。差异可能是几百个百分点,可能来自JVM优化。如果将数组变量声明为volatile,则会获得类似的性能。