[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: programing styles



Actually, no objects are created in the example, only the local variables
obj, y and i. The method call vect.elementAt(i) simply returns a reference
to an existing object. Even if it did create a new object, style 2 wouldn't
save any object creation cost since obj is assigned to null outside the
loop. However, style 2 does appear to save allocating the variables obj and
y on the stack each time through the loop. I wonder if the compiler
optimizes style 1 to eliminate even that difference.

Jim

-----Original Message-----
From: Mike Millson [mailto:mgm@atsga.com]
Sent: Friday, November 01, 2002 3:13 PM
To: ajug-members@ajug.org
Subject: RE: programing styles


Isn't #2 better for garbage collection, since you are not creating and
destroying an object on each iteration of the loop? My understanding is that
the number of objects you create directly impacts speeds. The less objects,
the less garbage collection, and the faster the application. Given that, I
would think #2 is much more efficient and the style to use.

Mike

-----Original Message-----
From: Magesh Umasankar [mailto:umagesh@apache.org]
Sent: Friday, November 01, 2002 2:42 PM
To: ajug-members@ajug.org
Subject: Re: programing styles


I would say the first one is - keep variable
declarations as local as possible - better
for garbage collection also, in most cases.

In your case, you gain nothing by declaring
them outside the for loop as you don't seem
to be using them outside the loop.

Cheers,
Magesh

**************************************************
*  The surest sign that intelligent life exists  *
*  elsewhere in the universe is the fact that it *
*  has never tried to contact us.                *
**************************************************
----- Original Message -----
From: "Gudavalli, Manidhar" <manidhar.gudavalli@eds.com>
To: <ajug-members@ajug.org>
Sent: Friday, November 01, 2002 2:34 PM
Subject: programing styles


>
>
>
> HI
>
> Which  style of coding is best ?
>
> [1]
> private void myproc(Vector vect){
> for (i=1; i < vect.size() ; i++){
>
> MyObject obj = (MyObject) vect.elementAt(i);
>   int y = obj.getY();
> }
> }
>
> or
> [2]
> private void myproc(Vector vect){
> MyObject obj = null;
> int y= 0;
>
> for (i=1; i < vect.size() ; i++){
>
>   obj = (MyObject) vect.elementAt(i);
>     y = obj.getY();
> }
> }
>