Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Efficient Code

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a for loop, if an equation is placed in the continuation condition is this equation evaluated everytime the condition is tested or only once, ie the first time the condition is tested.
In other words which of these is more efficient:
for ( i=0; i < (6 * b - 4 * a); i++) {} or
c = 6 * b - 4 * a;
for ( i = 0; i < c; i++) {}
Thanks for your help
Peter
 
Don Smathers
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why obfuscate, when simple will do ?
Cant answer your inquiry, in general, as perhaps this is too dependent on the compiler. I, personally, much prefer c = etc., -- makes the code more readable, and is probably easier to debug. Not sure what you would do, tho, in the case of an extra long boolean-- a series of && , | | --- my inclination would be to break it up.
The following code produced a series of #s, so from this I deduce that the 'stop test' is evaluated each time the loop is entered, so that your c = ' ' would be more efficient.
public class TestEm
{
public static void main ( String [ ] args )
{
int i = 9;
for (int j = 0; j <= i; j += 2 )
{
(i++);
System.out.print(" " + i );
} // 10 11 12 ....
}
}

[This message has been edited by Don Smathers (edited March 24, 2001).]
 
Andy Ceponis
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to agree with Don here. It is much simpler to read and debug the following example:

Plus if you use the first example you are causing the compiler to do alot of extra work by having to evaluate the (6 * b - 4 * a) expression everytime. So if that loop was say to 1000 or some large nnumber, then youd would lose valuable milliseconds. Hope that helps.
 
Junilu Lacar
Bartender
Posts: 7568
52
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andy,
I think readability would be the main consideration in this case. A good optimizing compiler could probably figure out whether or not the expression changes within the body of the loop. If it can prove that the expression isn't affected by anything that happens inside the loop, the compiler could produce executable code that performs just as well. If you are really concerned about performance, use a profiler, don't second guess the compiler.
Check out Martin Fowler's discussion of Refactoring and Performance (pg. 69) in his book "Refactoring."
J.Lacar
[This message has been edited by JUNILU LACAR (edited March 24, 2001).]
 
Andy Ceponis
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah that sounds about right.
I have no experience with different compilers. I was just taught to take every situation and assume extras like this would cause more work for the program/compiler. Thats good to know that certain compilers will/might figure stuff like this out.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys for your help.
Peter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic