Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-20-2008, 06:07 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-20-2008, 07:11 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-25-2008, 04:42 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
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 04:46 PM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-23-2008, 09:29 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 720
Nicholas Jordan is on a distinguished road
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 10:18 PM. Reason: "...keeps all items sorted...."
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Advice on best method for.... shaungoater Java 2D 1 06-23-2008 09:36 PM
Want Some Expert Advice? JAX India 2008 Might Be the Right Place james Java Announcements 2 03-03-2008 10:05 PM
Prerequisite advice needed Hatrabbit New To Java 2 11-30-2007 06:38 PM
First project, need some tips.. Komala_aradhya New To Java 1 08-03-2007 03:25 PM
Generating Code Automatically Using Custom code Template In Eclipse JavaForums Eclipse 1 04-26-2007 05:52 PM


All times are GMT +3. The time now is 11:27 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org