Dynamic Object Creation Vs Preallocation of Objects
Which code (A or B) do you think is efficient in Java?
Say I need to create an arraylist of objects to sum the values for each month.
-
Code A
GenericVO vo = null;
ArrayList list = new ArrayList();
for(int i=0;i<84;i++) { //for 7years
vo = new GenericVO (); //preallocation
list.add(vo);
}
If(values available for a month)
{
vo = list.getVO(month);
vo.setValue(value from db);
}
-
Code B
GenericVO vo = null;
ArrayList list = new ArrayList();
for(int i=0;i<84;i++) {
list.add(" "); //just to avoid indexout of bound exception
}
If(only values available for a month from db) {
vo = new GenericVO (); //create dynamically
list.add(month,vo);
}
-
I really don't know what you're trying to do here, but neither one of those is likely to be more performant to any significant degree. Go with the one that more clearly expresses your design intent.
jverda at 2007-7-13 20:01:35 >

> Which code (A or B) do you think is efficient in
Define efficient
> Say I need to create an arraylist of objects to sum
> the values for each month.
What values? Why do you need an arraylist to sum them?
> for(int i=0;i<84;i++) { //for 7years
>vo = new GenericVO (); //preallocation
>list.add(vo);
> }
ok so you've got an arraylist with 84 elements
> Code B
> GenericVO vo = null;
> ArrayList list = new ArrayList();
>for(int i=0;i<84;i++) {
> list.add(" "); //just to avoid indexout of bound
You've got an arraylist with 84 copies of one string
> If(only values available for a month from db) {
>vo = new GenericVO (); //create dynamically
>list.add(month,vo);
> }
We do not have enough information to determine what this does
Sorry for not being clear.
I just wanted to know if dynamically creating objects in Java is more memory consuming than creating the objects before in hand.
Some forums say that predifing/creating objects creates contiguous memory than creating objects dynamically.
Efficient in this context is better code of the two codes.
if an object is already available for the month, get the object for that month and add the new object value to the old object for that month
if there is no object for the month, just add the new object for that month.
You really haven't clarified anything, IMHO.
But I wouldn't worry about creating in contiguous blocks. Most modern VMs are probably smart enough not to need your help in that regard. There's an article I don't have handy right now that covers that. From IBM, I think. Something about Myth vs. Reality of Java Performance, GC, etc.
jverda at 2007-7-13 20:01:35 >

javastud12, you can do as I suggested before--use new ArrayList(84);. But, in any case, your code A and code B will have different final list sizes.
Code A:
Create 84 GenericVO objects, and set appropriate ones from the database.
Final result: 84 items in list, some of which may still be set at default values for a GenericVO
Code B:
Put 84 Strings in a list. For appropriate values, you "add" a GenericVO to the list.
Final result: 84 Strings in the list, plus some number of GenericVO items (however many you have info for in the database). Your month indexes for anything after the first month inserted (say, month 3 out of 84 [i.e., index 2 in the list]) are all wrong.
You probably want:
// set array to proper size to begin with--no need for ArrayList to expand later
List list = new ArrayList(84);
if(values available for a month)
{
// set item at month number
GenericVO vo = new GenericVO();
vo.setValue(value from db); // or pass data to constructor
list.set(monthIndex, vo); // use *set*, not *add*
}
MLRona at 2007-7-13 20:01:35 >

> You really haven't clarified anything, IMHO.
>
> But I wouldn't worry about creating in contiguous
> blocks. Most modern VMs are probably smart enough not
> to need your help in that regard. There's an article
> I don't have handy right now that covers that. From
> IBM, I think. Something about Myth vs. Reality of
> Java Performance, GC, etc.
http://www-128.ibm.com/developerworks/java/library/j-jtp01274.html
jverda at 2007-7-13 20:01:35 >

> > http://www-128.ibm.com/developerworks/java/library/j-j> tp01274.htmlVery interesting article thanks jverd.
Very nice article Thanks.