|
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 06:15 PM.
|