Results 1 to 10 of 10
Thread: slow iterator
- 05-23-2009, 03:42 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 6
- Rep Power
- 0
slow iterator
hi there,
I am trying to write a small method that generates a mysql command. but it looks like that this method uses 98% of the running time. I can't make it faster. I tried hashmap instead of arraylist, different iterators, no result.
thanks a lot.Java Code:public void test (int id, ArrayList<Integer> link){ String mySqlCommand = "INSERT INTO profiles_child (userid, link) VALUES"; for(int i=0;i<link.size();i++){ if (id != link.get(i)) mySqlCommand += "("+id+","+link.get(i)+"),"; } mySqlCommand = mySqlCommand.substring(0, mySqlCommand.length()-1); //removing last character mySqlCommand += ";"; addToDatabaseLink1(mySqlCommand); }
Tom
-
My guess is that the problem has nothing to do with the iteration and everything to do with the last method call above:
addToDatabaseLink1(...).
If you really want to know what is slowing you down, perhaps you can get your hands on a profiler. NetBeans I think has one in its IDE, and perhaps Eclipse does too, but I'm not sure.
- 05-23-2009, 08:34 PM #3
Member
- Join Date
- Dec 2007
- Posts
- 6
- Rep Power
- 0
actually the profiler says that this method uses most of the rescources, especially when it is dealing with a long arraylist...
-
Yes, but what part of this method? Again, I suggest that it's the part at the bottom and has nothing to do with iteration.
The only other thought I have (for what they are worth), if you find that it is the loop causing the problem then to use a StringBuilder here and not do String concatonation directly.
- 05-23-2009, 09:32 PM #5
Member
- Join Date
- Dec 2007
- Posts
- 6
- Rep Power
- 0
thanks a lot. stringbuilder sorted out the problem.
-
cool. You must be iterating through a lot of strings then.
- 05-24-2009, 07:51 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Best way to findout this, write a simple code for concatenation and compile it with the verbose option. Just for a single concatenation operation in String class and the StringBuilder, approximately you can see 10~20ms difference.
- 05-24-2009, 10:29 AM #8
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
Isn't a String compiled to a StringBuilder anyway? So what use would it be to replace String with StringBuilder in your code?
I die a little on the inside...
Every time I get shot.
-
- 05-25-2009, 08:04 AM #10
Hi,
One small suggestion is u can use PreparedStatement.No need to build n number of string like this.
1.just declare the query string as final and using preparestatement and set the values for userid and link.
final String insertQuery = "INSERT INTO PROFILES_CHILD (USERID, LINK) VALUES ( ?,?)";
-Regards
RamyaRamya:cool:
Similar Threads
-
Implementing Iterator
By Harb in forum New To JavaReplies: 13Last Post: 11-21-2009, 07:25 PM -
iterator
By venkatallu in forum Advanced JavaReplies: 3Last Post: 09-23-2008, 01:32 PM -
iterator issues
By orchid in forum New To JavaReplies: 2Last Post: 08-12-2008, 01:43 PM -
Iterator
By eva in forum New To JavaReplies: 0Last Post: 01-31-2008, 02:07 PM -
using Iterator with Vector
By Java Tip in forum Java TipReplies: 0Last Post: 11-13-2007, 10:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks