用于循环优化

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++) {

...

}

哪个表现更好(假设我有很多花),我猜应该是后者。


慕标5832272
浏览 382回答 3
3回答

一只甜甜圈

最好使用for-each循环 [更具可读性]for (Flower flower :flowers){&nbsp; &nbsp; //...}我已将指令转储javap用于以下代码:public void forLoop1() {&nbsp; &nbsp; List<String> lst = new ArrayList<String>();&nbsp; &nbsp; for (int i = 0; i < lst.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("hi");&nbsp; &nbsp; }}public void forLoop2() {&nbsp; &nbsp; List<String> lst = new ArrayList<String>();&nbsp; &nbsp; int size = lst.size();&nbsp; &nbsp; for (int i = 0; i < size; i++) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("hi");&nbsp; &nbsp; }}public void forLoop1();&nbsp; Code:&nbsp; &nbsp;0:&nbsp; &nbsp;new&nbsp; &nbsp; &nbsp;#2; //class java/util/ArrayList&nbsp; &nbsp;3:&nbsp; &nbsp;dup&nbsp; &nbsp;4:&nbsp; &nbsp;invokespecial&nbsp; &nbsp;#3; //Method java/util/ArrayList."<init>":()V&nbsp; &nbsp;7:&nbsp; &nbsp;astore_1&nbsp; &nbsp;8:&nbsp; &nbsp;iconst_0&nbsp; &nbsp;9:&nbsp; &nbsp;istore_2&nbsp; &nbsp;10:&nbsp; iload_2&nbsp; &nbsp;11:&nbsp; aload_1&nbsp; &nbsp;12:&nbsp; invokeinterface #4,&nbsp; 1; //InterfaceMethod java/util/List.size:()I&nbsp; &nbsp;17:&nbsp; if_icmpge&nbsp; &nbsp; &nbsp; &nbsp;34&nbsp; &nbsp;20:&nbsp; getstatic&nbsp; &nbsp; &nbsp; &nbsp;#5; //Field java/lang/System.out:Ljava/io/PrintStream;&nbsp; &nbsp;23:&nbsp; ldc&nbsp; &nbsp; &nbsp;#6; //String hi&nbsp; &nbsp;25:&nbsp; invokevirtual&nbsp; &nbsp;#7; //Method java/io/PrintStream.println:(Ljava/lang/String;)V&nbsp; &nbsp;28:&nbsp; iinc&nbsp; &nbsp; 2, 1&nbsp; &nbsp;31:&nbsp; goto&nbsp; &nbsp; 10&nbsp; &nbsp;34:&nbsp; returnpublic void forLoop2();&nbsp; Code:&nbsp; &nbsp;0:&nbsp; &nbsp;new&nbsp; &nbsp; &nbsp;#2; //class java/util/ArrayList&nbsp; &nbsp;3:&nbsp; &nbsp;dup&nbsp; &nbsp;4:&nbsp; &nbsp;invokespecial&nbsp; &nbsp;#3; //Method java/util/ArrayList."<init>":()V&nbsp; &nbsp;7:&nbsp; &nbsp;astore_1&nbsp; &nbsp;8:&nbsp; &nbsp;aload_1&nbsp; &nbsp;9:&nbsp; &nbsp;invokeinterface #4,&nbsp; 1; //InterfaceMethod java/util/List.size:()I&nbsp; &nbsp;14:&nbsp; istore_2&nbsp; &nbsp;15:&nbsp; iconst_0&nbsp; &nbsp;16:&nbsp; istore_3&nbsp; &nbsp;17:&nbsp; iload_3&nbsp; &nbsp;18:&nbsp; iload_2&nbsp; &nbsp;19:&nbsp; if_icmpge&nbsp; &nbsp; &nbsp; &nbsp;36&nbsp; &nbsp;22:&nbsp; getstatic&nbsp; &nbsp; &nbsp; &nbsp;#5; //Field java/lang/System.out:Ljava/io/PrintStream;&nbsp; &nbsp;25:&nbsp; ldc&nbsp; &nbsp; &nbsp;#6; //String hi&nbsp; &nbsp;27:&nbsp; invokevirtual&nbsp; &nbsp;#7; //Method java/io/PrintStream.println:(Ljava/lang/String;)V&nbsp; &nbsp;30:&nbsp; iinc&nbsp; &nbsp; 3, 1&nbsp; &nbsp;33:&nbsp; goto&nbsp; &nbsp; 17&nbsp; &nbsp;36:&nbsp; return它没有为我优化。Java版本“ 1.6.0_22” Java™SE运行时环境(内部版本1.6.0_22-b04)Java HotSpot(TM)客户端VM(内部版本17.1-b03,混合模式,共享)因此,如果您需要从上述两个中进行选择,请争取第二,但我个人会建议for-each。每个绩效摘自Joshua Bloch撰写的有效Java的条款46 :版本1.5中引入的for-each循环通过完全隐藏迭代器或index变量,摆脱了混乱和出错的机会。结果成语同样适用于集合和数组:// The preferred idiom for iterating over collections and arraysfor (Element e : elements) {&nbsp; &nbsp; doSomething(e);}当您看到冒号(:)时,将其读为“ in”。因此,上面的循环读为“针对元素中的每个元素e”。请注意,即使对于数组,使用for-each循环也不会降低性能。 。实际上,在某些情况下,它可能只比普通的for循环提供一点性能优势,因为它只计算一次数组索引的限制。尽管您可以手动执行此操作(第45项),但程序员并非总是如此。

呼如林

JVM无法优化它,因为它size()是一种方法,并且JVM无法(也不会尝试)确定size()在这种情况下总是返回相同的值。提供的size()值不会改变,第二个值的性能会更高,但是收益是如此,以至于您甚至不必考虑使用它。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java