aspose file tools*
The moose likes Beginning Java and the fly likes Efficient Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Efficient Code" Watch "Efficient Code" New topic
Author

Efficient Code

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Mar 04, 2001
Posts: 31
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

Joined: Dec 20, 2000
Posts: 782
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

Joined: Feb 26, 2001
Posts: 4456
    
    6

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).]


Junilu - [How to Ask Questions] [How to Answer Questions]
Andy Ceponis
Ranch Hand

Joined: Dec 20, 2000
Posts: 782
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

Joined: Nov 22, 2008
Posts: 18944
Thanks Guys for your help.
Peter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Efficient Code