for循环优化 [英] for loop optimization

查看:16
本文介绍了for循环优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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 循环 [更具可读性]

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

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

<小时>

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


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

它不适合我.

java 版本1.6.0_22"Java(TM) SE运行时环境(构建1.6.0_22-b04) Java HotSpot(TM) 客户端 VM(构建 17.1-b03,混合模式,分享)

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)

因此,如果您需要从上述两个中进行选择,请选择第二个,但我个人会选择 for-each.

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

来自 Joshua 的 Effective Java 中的第 46 条布洛赫:

From Item 46 in Effective Java by Joshua Bloch :

for-each 循环,引入发布 1.5,摆脱混乱以及出错的机会隐藏迭代器或索引变量完全地.结果成语同样适用于集合和数组:

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);
}

当您看到冒号 (:) 时,将其读作在."因此,上面的循环读作对于元素中的每个元素 e."笔记没有性能损失用于使用 for-each 循环,甚至 for数组.事实上,它可能会提供轻微的性能优于普通在某些情况下 for 循环,因为它计算数组索引的限制只有一次.虽然你可以通过手(第 45 项),程序员不会总是这样做.

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.

<小时>

另见

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆