Results 1 to 16 of 16
Thread: Improve the Speed of loop
- 02-11-2011, 10:46 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
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;
}
}
}
}
}
}
}
}
- 02-11-2011, 10:50 AM #2
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
- 02-11-2011, 11:16 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,401
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-11-2011, 11:50 AM #4
Member
- Join Date
- Jan 2011
- Posts
- 9
- Rep Power
- 0
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.
- 02-11-2011, 12:09 PM #5
Last edited by j2me64; 02-11-2011 at 12:44 PM.
- 02-11-2011, 12:39 PM #6
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
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
- 02-11-2011, 12:49 PM #7
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.
- 02-11-2011, 12:51 PM #8
It is bad too. You need do as followssb.append(s + strj + strk + strl + m + n + "\n");
Java Code:sb.append(s).append(strj).append(strk).append(strl).append(m).append(n).append("\n");Skype: petrarsentev
http://TrackStudio.com
- 02-11-2011, 01:05 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,401
- Blog Entries
- 7
- Rep Power
- 17
- 02-11-2011, 01:17 PM #10
Member
- Join Date
- Mar 2009
- Posts
- 70
- Rep Power
- 0
Declaring new String objects every time also can have a huge impact. Try and declare the variables outside the loops, and use them.
- 02-11-2011, 01:20 PM #11
- 02-11-2011, 01:40 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,401
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-11-2011, 01:51 PM #13
Last edited by j2me64; 02-11-2011 at 02:00 PM.
- 02-11-2011, 02:06 PM #14
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
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.
- 02-11-2011, 02:11 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,401
- Blog Entries
- 7
- Rep Power
- 17
- 02-16-2011, 06:36 AM #16
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
Is there any way to iterate the loop using multithreading and jdk 1.7.
Java Concurrency - Part 7 : Executors and thread pools | @Blog("Baptiste Wicht")
Similar Threads
-
Improve my GUI!
By AJArmstron@aol.com in forum New To JavaReplies: 8Last Post: 04-27-2010, 09:17 PM -
Improve write speed
By Krons in forum Advanced JavaReplies: 10Last Post: 04-16-2009, 06:30 AM -
How to speed sql Statements?
By bezudar in forum Advanced JavaReplies: 3Last Post: 11-20-2008, 09:53 AM -
how to improve the performance of JWS?
By dinesh kaushik in forum Java AppletsReplies: 0Last Post: 11-21-2007, 08:46 AM -
compare speed
By bbq in forum JDBCReplies: 1Last Post: 06-28-2007, 05:34 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks