This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Performance and the fly likes Reference declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Reference declaration" Watch "Reference declaration" New topic
Author

Reference declaration

Pablo Olmos
Greenhorn

Joined: Aug 20, 2004
Posts: 10
Hi,
How time expensive is it to declare references to objects inside methods? Is it better to declare them as private members of the class? What about simple types?

As a very simple example suppose that you inside a method you have five for-loops. what is better?
This:


or this?

Jeff Walker
Ranch Hand

Joined: Apr 25, 2004
Posts: 116
Pablo,
the only real difference I can see is that the first code block declares the i loop integer and the Iterator's in the for loops, and the second example declares them as separate instance variables.

I suspect a decent comiler can optimize away the difference.

In theory, when you write
for (int i = 0; i++...)
{}
the compiler treats it the same as
int i = 0;
for (i = 0; i ++...)
{}
anyway, so there would be no difference here.

Also, when you declare a method variable, you have to initialize it, which 'may' cause a time delay for complex objects. In your case, I suspect the compiler will generate exactly the same code for both examples. (You should be able to turn off this optimization in your IDE, but you won't see much of a difference until you start to initialize complex objects that take a measurable amount of time to initialize).

Now, if you declare them in the method, then you can use them after the for loops. If you declare them in the for loops, they will not be available outside of the for loops' scopes.

You also ask if they were to be declared at the class level (outside of methods), what impact that will have? Well, time-wise, I'd say again, very little, but technically instance variables are initialized to their defaults first, so

public class A
{
private int i;
...

}
causes the i variable to be initialized to zero at object creation (before constructor call).

Jeff Walker
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

call me pedantic, but compilers tend to be pedantic:
In theory, when you write
for (int i = 0; i++...)
{}
the compiler treats it the same as
int i = 0;
for (i = 0; i ++...)
{}
anyway, so there would be no difference here.

The compiler should treat it as:

or

with the scope of 'int i' being reduced to the loop.


http://home.arcor.de/hirnstrom/bewerbung
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Pablo,

welcome to the Ranch!

Please notice that we do have a naming policy at the Ranch. Please change your display name accordingly - the linked page tells you how to do so.

Thank you very much - and have much fun at the Ranch!


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Pablo Olmos
Greenhorn

Joined: Aug 20, 2004
Posts: 10
At run-time, what is better the first example or the second?

Explained in another way, does the program run slower if for each method invoked I declare and use a bunch of intermediate references/variables inside the method? or faster, if some of those references/variables were declared as member of the class that owns the method?

Pablo
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

You can write a test to find it out for your JVM and your kind of invoking it. That means, calling 'java -server YourProgram' might behave different, from not using the '-server'-flag.

The Hotspot-optimizing might result in other runtimes.
A 1.4.0 may behave different from a 1.4.2.
A linux-implementation might behave different from a MacOs.

Make a lot of tests, for different counts of calls.
Repeat those tests, to avoid random-results.

Read Knut: 'Premature optimization is the root of all evil'.

Most code needs a long time of maintainance, and variables moved to the class instead of a method:
a) confuse
b) might behave funny with multithreading
c) can easily cause developer-errors

But make some tests, to see, what you have to pay to avoid the drawbacks.
Kris Philippaerts
Greenhorn

Joined: Oct 19, 2001
Posts: 25
First, method variables are stored in the stack frame of your method. So every variable you declare will need a place in this frame. But when variables get out of scope in the method, this place can be used by an other variable that is declared further in the method.

Second, you should keep the scope of your variables as little as ^possible, at all times. If you don't do so, you might cause a STORE operation (for variables with a lifecycle larger than needed), and *that* would be a waste.

So, basically, it is better coding style to declare your variables in your for loop, and not outside. You should not trust the compiler to optimize your code since not all compilers will do so.

The same is true for iterators: you beter use:

for (Iterator=...

instead of:

Iterator iter = ...
while (iter.hasNext()) ...

HTH,
Kris
Pablo Olmos
Greenhorn

Joined: Aug 20, 2004
Posts: 10
Thanks for the advice. It's exactly what I needed to know.

Pablo
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Kris Philippaerts:
First, method variables are stored in the stack frame of your method.


I am not sure wether this is guaranteed by the Java specification. It is certainly the most common implementation, but I could also imagine the Hot Spot Engine to decide to use a register for a "very local" variable instead.

But if the stack frame is used, then it needs to be allocated anyway (for the method return address, parameters etc.) and will only be allocated at the method entry point. That is, the declaration of a local variable wouldn't cause any additional operation to be performed at all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reference declaration