Results 1 to 4 of 4
- 01-20-2008, 04:07 PM #1
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 04:15 PM.
- 01-20-2008, 05:11 PM #2
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.
- 01-25-2008, 02:42 PM #3
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 02:46 PM.
- 06-23-2008, 07:29 PM #4
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 08:18 PM. Reason: "...keeps all items sorted...."
Similar Threads
-
Advice on best method for....
By shaungoater in forum Java 2DReplies: 1Last Post: 06-23-2008, 07:36 PM -
Want Some Expert Advice? JAX India 2008 Might Be the Right Place
By james in forum Java SoftwareReplies: 2Last Post: 03-03-2008, 08:05 PM -
Prerequisite advice needed
By Hatrabbit in forum New To JavaReplies: 2Last Post: 11-30-2007, 04:38 PM -
First project, need some tips..
By Komala_aradhya in forum New To JavaReplies: 1Last Post: 08-03-2007, 01:25 PM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks