通过Java列表高效循环高效、列表、Java

2023-09-05 02:05:35 作者:李白洗碗用碧浪i

下表是从2008年谷歌I​​ / O谈所谓的Dalvik虚拟机内部件的方式来遍历一组对象,以便从大多数的列表,以效率最低的:

The following list is from the google I/O talk in 2008 called "Dalvik Virtual Machine Internals" its a list of ways to loop over a set of objects in order from most to least efficient:

(1) for (int i = initializer; i >=0; i--) //hard to loop backwards
(2) int limit = calculate_limit(); for (int i= 0; i< limit; i++)
(3) Type[] array = get_array(); for (Type obj : array)
(4) for (int i =0; i< array.length; i++) //gets array.length everytime
(5) for (int i=0; i < this.var; i++) //has to calculate what this.var is
(6) for (int i=0; i < obj.size(); i++) //even worse calls function  each time
(7) Iterable list = get_list(); for (Type obj : list) //generic object based iterators slow!

第3是效率的同一地区,避免7如果可能的话。这主要是咨询,帮助电池的寿命,但可能会帮助Java SE code了。

The first 3 are in the same territory of efficiency, avoid 7 if possible. This is mainly advice to help battery life but could potentially help java SE code too.

我的问题是:为什么(7)是缓慢的,所以(3)是很好的?我想这可能是(3)和(7)数组和List之间的差异。此外,丹提到的(7)创建必须GCed小的临时对象的负载,我有点生锈的关于Java的今天,有人可以解释为什么?这是在他的谈影片在0点41分10秒一分钟。

My question is: why (7) is slow and why (3) is good? I thought it might be the difference between Array and List for (3) and (7). Also, as Dan mentioned (7) creates loads of small temporary objects which have to be GCed, I'm a bit rusty on Java nowadays, can someone explain why? It's in his talk video at 0:41:10 for a minute.

推荐答案

这名单是有点过时了,今天不应该是真正有用的。

This list is a bit outdated, and shouldn't be really useful today.

这在几年前,当Android设备的速度很慢,并有非常有限的资源是一个很好的参考。 Dalvik虚拟机执行也缺乏了很多当今的优化。

It was a good reference some years ago, when Android devices were slow and had very limited resources. The Dalvik VM implementation also lacked a lot of optimizations available today.

在这样的设备,简单的垃圾收集顺手接过 1或2秒(与之相比,发生在今天,大多数的设备周围20毫秒)。在GC,该设备只是冻结,因此开发商必须非常小心内存消耗。

On such devices, a simple garbage collection easily took 1 or 2 seconds (for comparison, it takes around 20ms on most devices today). During the GC, the device just freezed, so the developers had to be very careful about memory consumption.

您应该不必过分担心,今天,但如果你真的关心性能,这里有一些细节:

You shouldn't have to worry too much about that today, but if you really care about performance, here are some details :

(1) for (int i = initializer; i >= 0; i--) //hard to loop backwards
(2) int limit = calculate_limit(); for (int i=0; i < limit; i++)
(3) Type[] array = get_array(); for (Type obj : array)

这些的人很容易理解。 I&GT; = 0 更快评估比 I&LT;限,因为它在做比较之前不读取变量的值。它可以直接与整数文字,这是更快。

These ones are easy to understand. i >= 0 is faster to evaluate than i < limit because it doesn't read the value of a variable before doing the comparison. It works directly with an integer literal, which is faster.

我不知道为什么(3)应该比慢(2)。编译器应该产生相同的环路(2),但也许是Dalvik虚拟机并没有在这个时候正确的优化。

I don't know why (3) should be slower than (2). The compiler should produce the same loop as (2), but maybe the Dalvik VM didn't optimize it correctly at this time.

(4) for (int i=0; i < array.length; i++) //gets array.length everytime
(5) for (int i=0; i < this.var; i++) //has to calculate what this.var is
(6) for (int i=0; i < obj.size(); i++) //even worse calls function  each time

这些的人都在评论中已经解释过。

These ones are already explained in the comments.

(7) Iterable list = get_list(); for (Type obj : list)

Iterables 是缓慢的,因为它们分配内存,做一些错误处理,在内部调用多种方法,......所有这一切比(6)只做慢得多在每次迭代一个函数调用。

Iterables are slow because they allocate memory, do some error handling, call multiple methods internally, ... All of this is much slower than (6) which does only a single function call on each iteration.

 
精彩推荐
图片推荐