Results 1 to 5 of 5
Thread: Optimization of code
- 08-16-2009, 08:43 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 17
- Rep Power
- 0
Optimization of code
Hi,
I need some help regarding the following code.
What I want is I am storing the word difference between 2 strings in an array m. and I want the least value from that array. the problem is that array is very big. so sorting it takes time. can anyone suggest how do i get the least word diff in shorter time?Java Code:public static void main(String[] args) throws IOException { //int mindiff = 100 , k = 0, diff = 0; String wordList[], words[]; breathalyzer b = new breathalyzer(); wordList = b.readWordList(); words = b.readInput(args[0]); int m[] = new int[wordList.length]; int i,j; for(i=0; i<words.length; i++) { for(j=0; j<wordList.length; j++) { if(words[i] != null && wordList[j] != null) { m[j] = b.wordDifference(words[i] , wordList[j]); } } } }//End of main
i wouldn't mind not using an array for this.
- 08-16-2009, 08:53 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
Why not store them in a SortedSet instead of an array? That way, they'd always be sorted...
Alternatively, you could loop over the array storing the minimum word diff as you go. It would probably be faster than sorting the whole list.Last edited by dlorde; 08-16-2009 at 08:56 PM.
- 08-16-2009, 08:55 PM #3
Member
- Join Date
- Aug 2009
- Posts
- 17
- Rep Power
- 0
great idea...will try that. but i hvnt used sortedset before.
can you please tell me how to use it.Last edited by new_coder; 08-16-2009 at 09:05 PM.
-
new coder, please do not double-post or cross-post a question. This is against the forum rules you agreed to on joining us and is not good forum etiquette. Thanks for your cooperation.
- 08-17-2009, 03:03 PM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
Just like any other SDK class - you go to the API docs (SortedSet), read them, and find the most suitable class that implements the interface (probably TreeSet). You then read the docs and look at the available methods you can use. It's a Collection class, so it basically works like any other Collection, but has its own special methods too.
Similar Threads
-
Optimization of code
By new_coder in forum New To JavaReplies: 1Last Post: 08-16-2009, 09:38 PM -
Query Optimization
By gilbertsavier in forum JDBCReplies: 0Last Post: 08-05-2009, 10:36 AM -
toHexString optimization (in fact general optimization question)
By jann in forum Advanced JavaReplies: 7Last Post: 12-16-2008, 06:44 PM -
java code optimization
By hey in forum New To JavaReplies: 0Last Post: 02-10-2008, 05:16 PM -
Optimization Algorithm Toolkit 1.3
By JavaBean in forum Java SoftwareReplies: 0Last Post: 07-14-2007, 08:24 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks