Home arrow static arrow Java Programming [Archive] - Performance tuning help
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Performance tuning help
This topic has 5 replies on 1 page.

Posts:6
Registered: 5/12/04
Performance tuning help  
Jun 28, 2004 3:25 AM



 
Pls guide whether any performance tuning can be achieved by replacing the former code with the latter.
___________________________________________________________

ArrayList bottles = new ArrayList();
HashMap bottle=null;
while(rs.next())
{
bottle = new HashMap();
bottle.put("bottle_no",rs.getString("bottle_no"));
bottle.put("bottle_pack_date",rs.getString("bottle_date"));
bottles.add(bottle);
}

____________________________________________________________

ArrayList bottles = new ArrayList();
HashMap bottle=new HashMap();
while(rs.next())
{
bottle.put("bottle_no",rs.getString("bottle_no"));
bottle.put("bottle_pack_date",rs.getString("bottle_date"));
bottles.add(bottle);
}

Thanks
 

Posts:2,909
Registered: 13.8.2003
Re: Performance tuning help  
Jun 28, 2004 3:39 AM (reply 1 of 5)



 
These two pieces of code aren't functionally equivalent, so no.

The first version works (creating an arraylist filled with several HashMaps that contain 2 items).
The second version is just plain wrong and it does things that you most certainly don't want to do.

Even so, your first version is still very bad design and I definitely wouldn't use a HashMap to store only 2 pairs of items.

Explain what you're trying to do and we'll help you.
 

Posts:11
Registered: 6/1/04
Re: Performance tuning help  
Jun 28, 2004 3:44 AM (reply 2 of 5)



 
Tip:

when you repeatdly do:

bottle = new HashMap() ;

you are unecessarily allocating new heap memory for nothing. (the new operator allocates memory)
 

Posts:450
Registered: 6/5/03
Re: Performance tuning help  
Jun 28, 2004 3:48 AM (reply 3 of 5)



 
why are you creating a List of Maps?
in your case, you need to add to List only a pair of values, bottle_no and bottle_date.
so you could use class similar to this one to add those thing to a List.
public class BottleInfo{  public final int no;  public final String date;  public BottleInfo(int no, String date) {    this.no  =  no;    this.date = date;  }}

or in case, when you need tu query those dates later by numbre, then you could just add all those bottles to a Map, so that "no" is key, and "date" is value.

in such case, you would cut down creation of one extra Map every loop iteration, and that would boost your codes' performance.

but the way you want to do it, i doubt it would work. since every iteration, the bottle would get new value, and therefore would all your Maps bottles in List bottles get new value, and discard the old one.
(please someone correct me if i'm wrong)
 

Posts:6
Registered: 5/12/04
Re: Performance tuning help  
Jun 29, 2004 12:32 AM (reply 4 of 5)



 
Actually this is the underlying design of the existing application. Can't do anything bout it as I am not authorised to do the same. All I have to do is find other ways n means to tune the code.
I basically want to avoid the unecessarily allocation of new heap memory for nothing. (the new operator allocates memory). Pls suggest ways to do the same.

 

Posts:2,909
Registered: 13.8.2003
Re: Performance tuning help  
Jun 29, 2004 1:24 AM (reply 5 of 5)



 
Actually this is the underlying design of the existing
application.

Have the person who designed it fired.

Can't do anything bout it as I am not authorised to do the same. All I have to do is find
other ways n means to tune the code.

Okay, how about finding the areas of code that take the longest time to execute?

I basically want to avoid the unecessarily allocation of new heap memory for nothing.

Well, your code doesn't allocate memory for "nothing" it uses it to create a bunch of HashMaps. It's stupid, but it's not "nothing".

(the new operator allocates memory).

Thank god it does, otherwise we would be pretty much screwed.

If you want better help, you really need to explain what you want to have as a result. If you don't want to have a list full of HashMaps say so. If you do want a list full of HashMaps, say it.
 
This topic has 5 replies on 1 page.