Results 1 to 4 of 4
  1. #1
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default I need some advice and tips about my code...

    Hello experts,

    Im currently developing my simple notepad that supports syntax highlighting...

    I've done the said feature but i really feel bad about the performance...

    Codes below:

    //Tested, (after 300 lines, performance starts to slow....
    //This is my problem...
    // Colors all keywords
    public void processChangedLines(int offset, int len) throws
    BadLocationException{
    String code = getText();
    ResetColor();

    Set<String> key = keywords.keySet();
    Color col = Color.blue;Pattern p;Matcher m;

    for (String k : key) {
    p = Pattern.compile("\\b"+k+"\\b");
    m = p.matcher(code);

    while(m.find()){
    StyleConstants.setForeground(style,col);
    setCharacterAttributes(m.start(),keyword.length(), style,false);
    }
    }
    }

    //Already tested (all keywords in a line x 10000), result: fast....
    private void ResetColor(){
    StyleConstants.setForeground(style,Color.black);
    setCharacterAttributes(0,getLength(),style,false);
    }

    The above method is called every pressed key....

    "database" is a storage for my keywords with colors... HashMap<String,Color>

    The algorithm i've implemented states,
    "Compare each keyword from database to all words in textpane"...
    //Slow performance

    I like to reimplement it by a new algorithm that states,
    "Compare each word from textpane to all keywords in the database"...
    //I believe it is faster than the old one..if im wrong, please correct me


    I need your advice experts how to reimplement it, I really feel bad about the performance... And most of all, I honestly have no idea how to reimplement it...
    I tried StringBuffer,StringBuilder,StringTokenizer but i can't...

    Does anyone here already encounter this kind of problem?
    Just a hint or algorithm or classes....


    Any reply will be greatly appreciated..
    Michael...
    Last edited by sukatoa; 01-20-2008 at 05:15 PM.

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    If yoi implemented your database as a hashtable and get color for every keyword in your document from the database with one query per each word, i think it should be fast enough. But when editing, you should check only the part being changed, not whole document. Let us know the result.

  3. #3
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default I got it....

    The performance improved...

    Hashmap implementation....

    Hashmap vs StringArray, which of them is the fastest to call?

    Any further advice?
    Last edited by sukatoa; 01-25-2008 at 03:46 PM.

  4. #4
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default Keys?

    For StringArray, there has to be some sort of sort or else we get an inefficient sequential search. HashSet Keeps all items sorted and is likely the fastest, but we do not have a key to getColor(); Likely TreeMap or HashMap:
    Last edited by Nicholas Jordan; 06-27-2008 at 09:18 PM. Reason: "...keeps all items sorted...."

Similar Threads

  1. Advice on best method for....
    By shaungoater in forum Java 2D
    Replies: 1
    Last Post: 06-23-2008, 08:36 PM
  2. Replies: 2
    Last Post: 03-03-2008, 09:05 PM
  3. Prerequisite advice needed
    By Hatrabbit in forum New To Java
    Replies: 2
    Last Post: 11-30-2007, 05:38 PM
  4. First project, need some tips..
    By Komala_aradhya in forum New To Java
    Replies: 1
    Last Post: 08-03-2007, 02:25 PM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 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
  •