Results 1 to 16 of 16
  1. #1
    venk123 is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default Improve the Speed of loop

    Hi How can improve the speed of the following code....

    import java.util.ArrayList;
    public class LoopTest {

    /**
    * @param args
    */



    public static void main(String[] args) {
    // TODO Auto-generated method stub

    ArrayList<String> temp = new ArrayList<String>();
    temp.add( "a1");
    temp.add( "b1");
    temp.add( "c1");
    temp.add( "d1");
    temp.add( "e1");
    temp.add( "f1");
    temp.add( "g1");
    temp.add( "h1");
    temp.add( "I1");
    temp.add( "j1");
    temp.add( "k1");
    temp.add( "l1");
    temp.add( "m1");
    temp.add( "n1");
    temp.add( "o1");
    temp.add( "p1");
    temp.add( "q1");
    temp.add( "r1");
    temp.add( "s1");
    temp.add( "t1");
    temp.add( "u1");
    temp.add( "v1");
    temp.add( "w1");
    temp.add( "x1");
    temp.add( "y1");
    temp.add( "z1");
    temp.add( "01");
    temp.add( "11");
    temp.add( "21");
    temp.add( "31");
    temp.add( "41");
    temp.add( "51");
    temp.add( "61");
    temp.add( "71");
    temp.add( "81");
    temp.add( "91");


    for(String str:temp){

    String a=str;


    for(String strj:temp){

    String b=a+strj;


    for(String strk:temp){

    String c=b+strk;


    for(String strl:temp){

    String d=c+strl;


    for(String m:temp){

    String e=d+m;


    for(String n:temp){

    String f=e+n;
    }


    }
    }


    }


    }

    }
    }



    }

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Well Why do you want doing this? I to see on your code and I don't understand what it do.
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,662
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by venk123 View Post
    Hi How can improve the speed of the following code....
    Those (six deep!) nested loops don't do anything with those local String variables so they can be removed. That leaves the local ArrayList<String> unused so it can also be removed. That leaves the main( ... ) method empty.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    mattie is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    I agree with JosAH. The nested for loops don't do anything as their variables are defined locally inside the loop, so unless you have something else in the loops that didn't copy here, they can be removed.

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by mattie View Post
    I agree with JosAH. The nested for loops don't do anything as their variables are defined locally inside the loop, so unless you have something else in the loops that didn't copy here, they can be removed.

    but the loops are nested, so the inner loops will have access to the outer variables. the problem is the number of permutations will be 2176782336.

    to the op: what is the code once you have your String f?
    Last edited by j2me64; 02-11-2011 at 12:44 PM.

  6. #6
    venk123 is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    I wanted to do a medical application. I wanted to analyze a raw image in java. I dont want to use any matlab like tools. I wanted to have permutation and combination of the image portion. So that my algorithm can detect ROI. It will be very help if somebody could solve my issue

  7. #7
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by venk123 View Post
    I wanted to do a medical application. I wanted to analyze a raw image in java. I dont want to use any matlab like tools. I wanted to have permutation and combination of the image portion. So that my algorithm can detect ROI. It will be very help if somebody could solve my issue

    i got a solution by eliminating one loop and building the string with

    Java Code:
    			for (String strj : temp) {
    				for (String strk : temp) {
    					for (String strl : temp) {
    						for (String m : temp) {
    								for (String n : temp) {
    									// here the string is written to a file
    									// change the code according to your requirements
    									sb.append(s + strj + strk + strl + m + n
    											+ "\n");

    where sb is a stringbuilder and s is the string constant, so that the first loop can be eliminated, reducing the number of loopings exponentially.
    Last edited by j2me64; 02-11-2011 at 01:04 PM.

  8. #8
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    sb.append(s + strj + strk + strl + m + n + "\n");
    It is bad too. You need do as follows
    Java Code:
    sb.append(s).append(strj).append(strk).append(strl).append(m).append(n).append("\n");
    Skype: petrarsentev
    http://TrackStudio.com

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,662
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by j2me64 View Post
    but the loops are nested, so the inner loops will have access to the outer variables. the problem is the number of permutations will be 2176782336.
    It has nothing to do with permutations; it is just counting where the 'digits' are a1, b1, c1 etc.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    rp181 is offline Member
    Join Date
    Mar 2009
    Posts
    70
    Rep Power
    0

    Default

    Declaring new String objects every time also can have a huge impact. Try and declare the variables outside the loops, and use them.

  11. #11
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by JosAH View Post
    It has nothing to do with permutations; it is just counting where the 'digits' are a1, b1, c1 etc.

    kind regards,

    Jos
    i don't see any counting in the code of the op, but perhaps you can show me where this happens.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,662
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by j2me64 View Post
    i don't see any counting in the code of the op, but perhaps you can show me where this happens.
    Yes it is counting; suppose that ArrayList contains the values '0', '1', '2' ... etc. Each nested loop iterates over all values; for the sake of the example asume two nested loops; they will generate the values 00, 01, 02 ... 09, 10, 11, 12 ... 19 ... 90, 91, 92 ...99. IOW it counts to 100. The original example counts in a similar way using different 'digits'.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by JosAH View Post
    Yes it is counting; suppose that ArrayList contains the values '0', '1', '2' ... etc. Each nested loop iterates over all values; for the sake of the example asume two nested loops; they will generate the values 00, 01, 02 ... 09, 10, 11, 12 ... 19 ... 90, 91, 92 ...99. IOW it counts to 100. The original example counts in a similar way using different 'digits'.

    kind regards,

    Jos

    ok, but once all "permutation and combination" (or counting :)) are done, what then?
    Last edited by j2me64; 02-11-2011 at 02:00 PM.

  14. #14
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by venk123 View Post
    Hi How can improve the speed of the following code....
    Assuming you have stripped out of this what you actually want to do with this, I will assume that you do actually intend something and so tell you to use a single StringBuilder and its append method rather than creating and discarding 36^6 StringBuffers and Strings (which is what you are doing with the "+" operator).
    Last edited by masijade; 02-11-2011 at 02:09 PM.

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,662
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by j2me64 View Post
    ok, but once all "permutation and combination" (or counting :)) are done, what then?
    I don't understand your question, sorry.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    venk123 is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

Similar Threads

  1. Improve my GUI!
    By AJArmstron@aol.com in forum New To Java
    Replies: 8
    Last Post: 04-27-2010, 09:17 PM
  2. Improve write speed
    By Krons in forum Advanced Java
    Replies: 10
    Last Post: 04-16-2009, 06:30 AM
  3. How to speed sql Statements?
    By bezudar in forum Advanced Java
    Replies: 3
    Last Post: 11-20-2008, 09:53 AM
  4. how to improve the performance of JWS?
    By dinesh kaushik in forum Java Applets
    Replies: 0
    Last Post: 11-21-2007, 08:46 AM
  5. compare speed
    By bbq in forum JDBC
    Replies: 1
    Last Post: 06-28-2007, 05:34 PM

Posting Permissions

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