Results 1 to 6 of 6
Thread: Performance Help
- 09-10-2010, 05:01 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
Performance Help
Hello I'll need some help to reduce the execution time of this 2 methods
andJava Code:/** * * @param textData text where the words will be replaced * @param designations List<Map<String, String>> containing the words to be replaced and is replacement * @return */ private String createConceptLinks(final String textData, final List<Map<String, String>> designations) { String retString = textData; for(Map<String, String> map : designations){ retString = replaceForLink(retString, map.get("designation"), map.get("replacement")); } return retString; }
this runs on a avg of 188ms. the thing is that I run it 100 times, so the total runing time is 18800ms which is a lot :(.Java Code:private String replaceForLink(final String text, final String word, final String replacement) { String newText = text; if(newText.toUpperCase(Constants.LOCALE_PT).contains(word.toUpperCase(Constants.LOCALE_PT))){ final Pattern pattern = Pattern.compile("(?:<([aA][A-Za-z0-9/=\\\":]*)[^>]*>)[�“\\!\\\"\\#\\$\\%\\&\\'\\(\\)\\*\\+\\,\\-\\.\\/\\:\\;\\=\\?\\@\\[\\\\\\]\\^\\_\\`\\{\\|\\}\\~\\s\\w]*[�“\\!\\\"\\#\\$\\%\\&\\'\\(\\)\\*\\+\\,\\-\\.\\/\\:\\;\\=\\?\\@\\[\\\\\\]\\^\\_\\`\\{\\|\\}\\~\\s\\w\\s\\w]*</[Aa\\s]*>|[a-zA-Z[&[a-z]+;]]+"+word+"[a-zA-Z[&[a-z]+;]]+|[a-zA-Z[&[a-z]+;]]*"+word+"[a-zA-Z[&[a-z]+;]]+|[a-zA-Z[&[a-z]+;]]+"+word+"[a-zA-Z[&[a-z]+;]]*",Pattern.CASE_INSENSITIVE); final Pattern pattern3 = Pattern.compile(word, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(newText); final List<String> list = new ArrayList<String>(); while (matcher.find()) { list.add(matcher.group()); } int i = 0; final String key1 = String.valueOf(new Date().getTime()); for(String string : list){ newText = newText.replaceFirst(string.replaceAll("\\?", "\\\\?"), "{"+key1+i+"}"); i++; } final List<String> list3 = new ArrayList<String>(); matcher = pattern3.matcher(newText); while (matcher.find()) { list3.add(matcher.group()); } i=0; for(String string : list3){ newText = newText.replaceFirst(string, replacement.replaceAll("@word@", string)); i++; } i = 0; for(String string : list){ newText=newText.replaceFirst("\\{"+key1+i+"\\}", string); i++; } } return newText; }
can anyone give-me a alternative assuming that i need to run it 100timesLast edited by blackHeart; 09-10-2010 at 05:04 PM.
- 09-10-2010, 05:24 PM #2
Remove the regexp code. That stuff is SLOW.
- 09-10-2010, 05:39 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
I need that! I don't want to replace if the word is between html tags and I also don't want to replace if is not the complete word.
e.g.
Java Code:in: Lorem ipsum dolor [B]lorem[/B]sit amet, <p>Lorem</p> consectetuer adipiscing elit. Aenean commodo ligula eget dolor. out "Replaced Lorem" ipsum dolor [B]lorem[/B]sit amet, <p>Lorem</p> consectetuer adipiscing elit. Aenean commodo ligula eget dolor.
- 09-10-2010, 06:05 PM #4
When I ran your code, by calling it with:
final static String Input = "Lorem ipsum dolor loremsit amet, <p>Lorem</p> consectetuer adipiscing elit."
+ " Aenean commodo ligula eget dolor.";
replaceForLink(Input, "lorem", "")
it returned:
ipsum dolor loremsit amet, <p></p> consectetuer adipiscing elit. Aenean commodo ligula eget dolor.
- 09-11-2010, 02:54 AM #5
Here is a simple example of using String methods vs regex. Loop time was 15ms for String, 500ms for regex
Java Code:import java.util.*; import java.util.regex.*; public class PatternMatcherTest { final static String Input = "Lorem ipsum dolor loremsit amet, <p>Lorem</p> consectetuer adipiscing elit." + " Aenean commodo ligula eget dolor. lorem "; final static String LookFor = "lorem"; final static String RepWith = ""; final static int NbrTests = 1000; // Number of loops for timing test //-------------------------------------------------------------------------------- public static void main(String[] args) { System.out.println("Input=" + Input + "<"); long start = System.currentTimeMillis(); // save for(int i=0; i < NbrTests; i++) { String results = replaceForLink(Input, LookFor , RepWith); if(i == 0) System.out.println("results=" + results + "<"); //results= ipsum dolor loremsit amet, <p></p> consectetuer adipiscing elit. Aenean commodo ligula eget dolor. } // 100 500 1000 System.out.println("time = " + (System.currentTimeMillis() - start)); //time = 15, 46, 141, 297,313 484 //---------------------------------------------- // Now do it manually with indexOf and substring start = System.currentTimeMillis(); // save start time for(int i=0; i < NbrTests; i++) { //Normalize data to all uppercase String workInput = Input.toUpperCase(); String workLookFor = LookFor.toUpperCase(); String results = Input; // start with full copy // StringBuilder results = new StringBuilder(Input); // Work end to front to preserve position as we substring int lastIx = workInput.length(); // set index at end while(true) { int ix = workInput.lastIndexOf(workLookFor, lastIx); // find last one if(ix < 0) break; // done if no more found // System.out.println("found at " + ix); lastIx = ix - workLookFor.length(); // advance start of scan ptr if((ix + workLookFor.length() == workInput.length()) // at end of string && (workInput.charAt(ix-1) == ' ')) { results = results.substring(0, ix); // drop ending word // results.setLength(ix); // System.out.println("stripped at end >" + results + "<"); // Is it a word between two spaces }else if((ix > 0) && (workInput.charAt(ix-1) == ' ') && (workInput.charAt(ix+workLookFor.length()) == ' ')) { // Get the before and after parts results = results.substring(0,ix) + results.substring(ix+workLookFor.length()); // results = results.delete(ix, ix+workLookFor.length()); }else if((ix == 0) && (workInput.charAt(workLookFor.length()) == ' ')) { // Strip off leading word results = results.substring(workLookFor.length()); // results = results.delete(0, workLookFor.length()); // Check if before is > and after is < }else if((workInput.charAt(ix-1) == '>') && (workInput.charAt(ix+workLookFor.length()) == '<')) { // System.out.println("leaving word between > & < ix=" + ix); } } // end while() if(i == 0) System.out.println("results=" + results + "<"); } // end for() // 100 500 1000 System.out.println("time = " + (System.currentTimeMillis() - start)); //time = 0, 15, 0-16, 15 16-32 } // end main() //--------------------------------------------------------------------------------------------------- static String replaceForLink(final String text, final String word, final String replacement) { String newText = text; // if(newText.toUpperCase(Constants.LOCALE_PT).contains(word.toUpperCase(Constants.LOCALE_PT))){ final Pattern pattern = Pattern.compile("(?:<([aA][A-Za-z0-9/=\\\":]*)[^>]*>)[�“\\!\\\"\\#\\$\\%\\&\\'\\(\\)\\*\\+\\,\\-\\.\\/\\:\\;\\=\\?\\@\\[\\\\\\]\\^\\_\\`\\{\\|\\}\\~\\s\\w]*[�“\\!\\\"\\#\\$\\%\\&\\'\\(\\)\\*\\+\\,\\-\\.\\/\\:\\;\\=\\?\\@\\[\\\\\\]\\^\\_\\`\\{\\|\\}\\~\\s\\w\\s\\w]*</[Aa\\s]*>|[a-zA-Z[&[a-z]+;]]+"+word+"[a-zA-Z[&[a-z]+;]]+|[a-zA-Z[&[a-z]+;]]*"+word+"[a-zA-Z[&[a-z]+;]]+|[a-zA-Z[&[a-z]+;]]+"+word+"[a-zA-Z[&[a-z]+;]]*",Pattern.CASE_INSENSITIVE); final Pattern pattern3 = Pattern.compile(word, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(newText); final List<String> list = new ArrayList<String>(); while (matcher.find()) { list.add(matcher.group()); } int i = 0; final String key1 = String.valueOf(new Date().getTime()); for(String string : list){ newText = newText.replaceFirst(string.replaceAll("\\?", "\\\\?"), "{"+key1+i+"}"); i++; } final List<String> list3 = new ArrayList<String>(); matcher = pattern3.matcher(newText); while (matcher.find()) { list3.add(matcher.group()); } i=0; for(String string : list3){ newText = newText.replaceFirst(string, replacement.replaceAll("@word@", string)); i++; } i = 0; for(String string : list){ newText=newText.replaceFirst("\\{"+key1+i+"\\}", string); i++; } // } return newText; } // end replaceForLink() // */ } // end class
- 09-13-2010, 09:24 AM #6
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
IO and Performance
By Cbani in forum New To JavaReplies: 12Last Post: 03-10-2010, 09:36 AM -
Performance problem
By varann in forum New To JavaReplies: 27Last Post: 09-11-2009, 10:15 AM -
performance is degraded in 1.6
By jagadeeshchinni in forum New To JavaReplies: 1Last Post: 10-22-2008, 04:24 PM -
Performance issue
By mathes_n in forum Web FrameworksReplies: 8Last Post: 09-02-2008, 05:11 AM -
performance problem on osx
By coldnebo in forum Advanced JavaReplies: 3Last Post: 08-01-2008, 09:39 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks