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

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

问题描述

全部,

在浏览 Java API 中的一些文件时,我注意到许多情况下循环计数器正在减少而不是增加.即在 forwhile 循环中 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).并且在每次迭代中都完成了comaprision.所以如果有任何(轻微的)性能提升,我认为这是因为与 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天全站免登陆