Results 1 to 10 of 10

Thread: slow iterator

  1. #1
    jamborta is offline Member
    Join Date
    Dec 2007
    Posts
    6
    Rep Power
    0

    Default 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.

    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);
    
        }
    thanks a lot.
    Tom

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    jamborta is offline Member
    Join Date
    Dec 2007
    Posts
    6
    Rep Power
    0

    Default

    actually the profiler says that this method uses most of the rescources, especially when it is dealing with a long arraylist...

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  5. #5
    jamborta is offline Member
    Join Date
    Dec 2007
    Posts
    6
    Rep Power
    0

    Default

    thanks a lot. stringbuilder sorted out the problem.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    cool. You must be iterating through a lot of strings then.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  8. #8
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    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.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Supamagier View Post
    Isn't a String compiled to a StringBuilder anyway? So what use would it be to replace String with StringBuilder in your code?
    I think it has something to do with reducing the number of objects created within the loop.

  10. #10
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    806
    Rep Power
    6

    Default

    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
    Ramya
    Ramya:cool:

Similar Threads

  1. Implementing Iterator
    By Harb in forum New To Java
    Replies: 13
    Last Post: 11-21-2009, 07:25 PM
  2. iterator
    By venkatallu in forum Advanced Java
    Replies: 3
    Last Post: 09-23-2008, 01:32 PM
  3. iterator issues
    By orchid in forum New To Java
    Replies: 2
    Last Post: 08-12-2008, 01:43 PM
  4. Iterator
    By eva in forum New To Java
    Replies: 0
    Last Post: 01-31-2008, 02:07 PM
  5. using Iterator with Vector
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-13-2007, 10:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •