for循环优化(for loop optimization)

编程入门 行业动态 更新时间:2024-10-15 20:16:23
for循环优化(for loop optimization) List<String> flowers = new ArrayList<String>();

我的for循环目前看起来像这样...

for (int i = 0; i < flowers.size(); i++) { ... }

或者我应该将其更改为下面给出的代码

int size = flowers.size(); for (int i = 0; i < size; i++) { ... }

哪个更好的表现(假设我有大量的花),我猜这应该是后者。

List<String> flowers = new ArrayList<String>();

My for loop currently looks like this...

for (int i = 0; i < flowers.size(); i++) { ... }

OR should I change this to look like the code given below

int size = flowers.size(); for (int i = 0; i < size; i++) { ... }

Which is more performant (assuming I have a large array of flowers), I am guessing it should be the latter.

最满意答案

最好使用for-each循环 [更可读]

for (Flower flower :flowers){ //... }

我已经使用javap转储了以下代码:

public void forLoop1() { List<String> lst = new ArrayList<String>(); for (int i = 0; i < lst.size(); i++) { System.out.println("hi"); } } public void forLoop2() { List<String> lst = new ArrayList<String>(); int size = lst.size(); for (int i = 0; i &t; size; i++) { System.out.println("hi"); } }
public void forLoop1(); Code: 0: new #2; //class java/util/ArrayList 3: dup 4: invokespecial #3; //Method java/util/ArrayList."<init>":()V 7: astore_1 8: iconst_0 9: istore_2 10: iload_2 11: aload_1 12: invokeinterface #4, 1; //InterfaceMethod java/util/List.size:()I 17: if_icmpge 34 20: getstatic #5; //Field java/lang/System.out:Ljava/io/PrintStream; 23: ldc #6; //String hi 25: invokevirtual #7; //Method java/io/PrintStream.println:(Ljava/lang/Str ing;)V 28: iinc 2, 1 31: goto 10 34: return public void forLoop2(); Code: 0: new #2; //class java/util/ArrayList 3: dup 4: invokespecial #3; //Method java/util/ArrayList."<init>":()V 7: astore_1 8: aload_1 9: invokeinterface #4, 1; //InterfaceMethod java/util/List.size:()I 14: istore_2 15: iconst_0 16: istore_3 17: iload_3 18: iload_2 19: if_icmpge 36 22: getstatic #5; //Field java/lang/System.out:Ljava/io/PrintStream; 25: ldc #6; //String hi 27: invokevirtual #7; //Method java/io/PrintStream.println:(Ljava/lang/Str ing;)V 30: iinc 3, 1 33: goto 17 36: return

它对我来说并不优化。

Java版本“1.6.0_22”Java(TM)SE运行时环境(构建1.6.0_22-b04)Java HotSpot(TM)客户端虚拟机(构建17.1-b03,混合模式,共享)

所以如果你需要从上面提到的两个人中选择一个,那么第二个,但是我个人会去做。


为每个表演

从有效Java中的第46项Joshua Bloch:

1.5版中引入的for-each循环通过完全隐藏迭代器或索引变量来摆脱杂乱和错误的机会。 所得成语同样适用于集合和数组:

// The preferred idiom for iterating over collections and arrays for (Element e : elements) { doSomething(e); }

当您看到冒号(:)时,将其读为“in”。因此,上面的循环读取为“元素中的每个元素e”。请注意,对于使用for-each循环,即使是数组也不会造成性能损失。 实际上,在某些情况下,它可能会比普通的for循环提供轻微的性能优势,因为它只计算一次数组索引的限制。 虽然你可以手动(项目45),程序员并不总是这样做。


也可以看看

IS-有-A-性能差之间-A-for循环和-A-的for-each循环

It is better to use for-each loop [more readable]

for (Flower flower :flowers){ //... }

I have dumped instructions using javap for the following code:

public void forLoop1() { List<String> lst = new ArrayList<String>(); for (int i = 0; i < lst.size(); i++) { System.out.println("hi"); } } public void forLoop2() { List<String> lst = new ArrayList<String>(); int size = lst.size(); for (int i = 0; i < size; i++) { System.out.println("hi"); } }
public void forLoop1(); Code: 0: new #2; //class java/util/ArrayList 3: dup 4: invokespecial #3; //Method java/util/ArrayList."<init>":()V 7: astore_1 8: iconst_0 9: istore_2 10: iload_2 11: aload_1 12: invokeinterface #4, 1; //InterfaceMethod java/util/List.size:()I 17: if_icmpge 34 20: getstatic #5; //Field java/lang/System.out:Ljava/io/PrintStream; 23: ldc #6; //String hi 25: invokevirtual #7; //Method java/io/PrintStream.println:(Ljava/lang/Str ing;)V 28: iinc 2, 1 31: goto 10 34: return public void forLoop2(); Code: 0: new #2; //class java/util/ArrayList 3: dup 4: invokespecial #3; //Method java/util/ArrayList."<init>":()V 7: astore_1 8: aload_1 9: invokeinterface #4, 1; //InterfaceMethod java/util/List.size:()I 14: istore_2 15: iconst_0 16: istore_3 17: iload_3 18: iload_2 19: if_icmpge 36 22: getstatic #5; //Field java/lang/System.out:Ljava/io/PrintStream; 25: ldc #6; //String hi 27: invokevirtual #7; //Method java/io/PrintStream.println:(Ljava/lang/Str ing;)V 30: iinc 3, 1 33: goto 17 36: return

It doesn't optimize for me.

java version "1.6.0_22" Java(TM) SE Runtime Environment (build 1.6.0_22-b04) Java HotSpot(TM) Client VM (build 17.1-b03, mixed mode, sharing)

So if you need to choose from mentioned two, go for second, but I personally would go for for-each.


for-each Performance

From Item 46 in Effective Java by Joshua Bloch :

The for-each loop, introduced in release 1.5, gets rid of the clutter and the opportunity for error by hiding the iterator or index variable completely. The resulting idiom applies equally to collections and arrays:

// The preferred idiom for iterating over collections and arrays for (Element e : elements) { doSomething(e); }

When you see the colon (:), read it as “in.” Thus, the loop above reads as “for each element e in elements.” Note that there is no performance penalty for using the for-each loop, even for arrays. In fact, it may offer a slight performance advantage over an ordinary for loop in some circumstances, as it computes the limit of the array index only once. While you can do this by hand (Item 45), programmers don’t always do so.


See Also

Is-there-a-performance-difference-between-a-for-loop-and-a-for-each-loop

更多推荐

本文发布于:2023-07-28 06:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1301657.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:loop   optimization

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!