Java API中的循环计数器 [英] Loop counter in Java API

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

问题描述

全部,

在浏览Java API中的一些文件时,我注意到许多实例,其中循环计数器正在递减而不是递增。即在中为,而 String 类。虽然这可能是微不足道的,但递减计数器而不是递增有什么意义吗?

While going through some of the files in Java API, I noticed many instances where the looping counter is being decremented rather than increment. i.e. in for and while loops in String class. Though this might be trivial, is there any significance for decrementing the counter rather than increment?

推荐答案

我编译了两个简单的循环用eclipse 3.6(java 6)看看字节码是否有一些差别。这是代码:

I've compiled two simple loops with eclipse 3.6 (java 6) and looked at the byte code whether we have some differences. Here's the code:

for(int i = 2; i >= 0; i--){}
for(int i = 0; i <= 2; i++){}

这是字节码:

// 1st for loop - decrement 2 -> 0
 0 iconst_2
 1 istore_1      // i:=2
 2 goto 8
 5 inc 1 -1      // i+=(-1)
 8 iload_1
 9 ifge 5        // if (i >= 0) goto 5

// 2nd for loop - increment 0 -> 2
12 iconst_0 
13 istore_1      // i:=0
14 goto 20
17 inc 1 1       // i+=1
20 iload_1
21 iconst 2
22 if_icmple 17  // if (i <= 2) goto 17

递增/递减操作应该没有区别,它是 +1 +( - 1)。这个典型(!)示例的主要区别在于,在第一个示例中,我们比较为0 ifge i ),在第二个示例中我们比较值 if_icmple i 2 )。并且每次迭代都会完成同样的事情。所以如果有任何(轻微的)性能增益,我认为这是因为与0进行比较然后与其他值进行比较的成本更低。所以我猜这不是增量/减量,而是停止标准

The increment/decrement operation should make no difference, it's either +1 or +(-1). The main difference in this typical(!) example is that in the first example we compare to 0 (ifge i), in the second we compare to a value (if_icmple i 2). And the comaprision is done in each iteration. So if there is any (slight) performance gain, I think it's because it's less costly to compare with 0 then to compare with other values. So I guess it's not incrementing/decrementing that makes the difference but the stop criteria.

所以,如果你需要在源代码级别上进行一些微优化,尝试以与零比较的方式编写循环,否则保持尽可能可读(并且递增更容易理解):

So if you're in need to do some micro-optimization on source code level, try to write your loops in a way that you compare with zero, otherwise keep it as readable as possible (and incrementing is much easier to understand):

 for (int i =  0; i <= 2; i++) {}  // readable
 for (int i = -2; i <= 0; i++) {}  // micro-optimized and "faster" (hopefully)

加法

昨天我做了一个非常基本的测试 - 刚刚创建了一个2000x2000阵列并填充了基于有关单元格索引的计算,一旦从 0-> 1999 计算行和单元格,另一次从 1999-> 0 。我并不感到惊讶,两个场景都有类似的性能(在我的机器上185 ... 210毫秒)。

Yesterday I did a very basic test - just created a 2000x2000 array and populated the cells based on calculations with the cell indices, once counting from 0->1999 for both rows and cells, another time backwards from 1999->0. I wasn't surprised that both scenarios had a similiar performance (185..210 ms on my machine).

所以,字节代码级别(eclipse 3.6)有差异但是,嘿,我们现在在2010年,它没有现在好像差别很大。再次,使用斯蒂芬斯的话,不要浪费你的时间这种优化。保持代码可读和易懂。

So yes, there is a difference on byte code level (eclipse 3.6) but, hey, we're in 2010 now, it doesn't seem to make a significant difference nowadays. So again, and using Stephens words, "don't waste your time" with this kind of optimization. Keep the code readable and understandable.

这篇关于Java API中的循环计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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