Best idiom for decrementing cycle

What is the best / preferred idiom for a contraction cycle for a cycle and why?

for(int idx=(len-1); idx>=0; idx--) {...} 

or

 for(int idx=(len-1); idx>-1; idx--) {...} 

or (based on sylvarking answer, but using to limit the size of the index)

 for(int idx=len; idx-->0; ) {...} 

Can one or the other be more likely to disable another developer?

+4
source share
9 answers

I recommend the following because comparison with zero is optimized at the byte code level.

 for(int idx=(len-1); idx>=0; idx--) {...} 

This offer is from Jack Shirazi's Java Performance Tuning

+11
source

Another array-oriented option is to see the following:

 int n = foo.length; while (n-- > 0) { /* Do something with foo[n] */ } 

Of the parameters that you show, I prefer the first.

  • 0 (possibly) more readable than -1 .
  • Comparing with zero is usually faster than comparing with other constants. This is one command that considers a single value on the stack. Other comparisons use one command to push a constant onto the stack and another command to compare the top two values.
+11
source

I canโ€™t say that I have ever seen the latter, therefore there is one vote for the first.

There should be no difference in performance.

Note that your loops are repeated using max + 1 elements. I usually assume that iteration is from max-1 to 0.

+1
source

Interest Ask! Personally, I use the first, and this is the one that I see most for other programmers. I think the reason is that people are used to testing zero, and -1 might throw them a little.

+1
source

The first; This is the standard way.

0
source

The second one still confuses me.
The first (more or less) that expects, in the decrement cycle for the cycle, it is not necessary to check> -1, so as not to be equal.

Personally, I would do it as Mark Byers .

0
source

My vote for:

for (int idx = (len-1); idx> = 0; idx -)

And as an aside, I prefer the prefix increment / decment operator (--idx) in for for-loops, as it will avoid an unnecessary copy of idx (although many compilers will probably find out and fix it for you).

0
source

I found that a for-loop with a counter should go only from zero to a certain limit.

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

It is so well known that you can expect that whoever sees this will immediately understand what is happening. It also means that any DEVIATION from this form, counting backward or starting with one or stepping on three, makes it difficult to understand, because you need to recognize that it is different, disassemble it and understand. This includes all of your examples.

I would suggest writing it clearly:

 for(int i = 0; i < MAX; i++) { indexFromEnd = (MAX - 1) - i; .... } 
-one
source

If you have a list, use ListIterator:

The following example is based on http://www.java-samples.com/showtutorial.php?tutorialid=235

 class ListIter { public static void main(String args[]) { // create an array list ArrayList al = new ArrayList(); // add elements to the array list al.add("A"); al.add("B"); al.add("C"); al.add("D"); al.add("E"); al.add("F"); // use ListIterator to display contents of al backwards ListIterator litr = al.listIterator(al.size()); while (litr.hasPrevious()) { Object element = litr.previous(); System.out.print(element + " "); } System.out.println(); } } 
-2
source

All Articles