Results 1 to 5 of 5
  1. #1
    new_coder is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Post Optimization of code

    Hi,

    I need some help regarding the following code.

    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
    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?

    i wouldn't mind not using an array for this.

  2. #2
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    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 09:56 PM.

  3. #3
    new_coder is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Default

    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 10:05 PM.

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

    Default

    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.

  5. #5
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by new_coder View Post
    ... i hvnt used sortedset before.
    can you please tell me how to use it.
    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

  1. Optimization of code
    By new_coder in forum New To Java
    Replies: 1
    Last Post: 08-16-2009, 10:38 PM
  2. Query Optimization
    By gilbertsavier in forum JDBC
    Replies: 0
    Last Post: 08-05-2009, 11:36 AM
  3. Replies: 7
    Last Post: 12-16-2008, 07:44 PM
  4. java code optimization
    By hey in forum New To Java
    Replies: 0
    Last Post: 02-10-2008, 06:16 PM
  5. Optimization Algorithm Toolkit 1.3
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-14-2007, 09:24 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
  •