Results 1 to 6 of 6
  1. #1
    blackHeart is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default Performance Help

    Hello I'll need some help to reduce the execution time of this 2 methods

    Java 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;
        }
    and

    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;
    	}
    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 :(.

    can anyone give-me a alternative assuming that i need to run it 100times
    Last edited by blackHeart; 09-10-2010 at 05:04 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    Remove the regexp code. That stuff is SLOW.

  3. #3
    blackHeart is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Remove the regexp code. That stuff is SLOW.
    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.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    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.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default

    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

  6. #6
    blackHeart is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    Norm
    I've runed your test and got the same results I'll analise it now.
    but I think you got the answer i was looking for.
    thanks for the time.

    my example works the problem is ">)[?“\" those are strange chars that appeared when I encoded on UTF-8

Similar Threads

  1. IO and Performance
    By Cbani in forum New To Java
    Replies: 12
    Last Post: 03-10-2010, 09:36 AM
  2. Performance problem
    By varann in forum New To Java
    Replies: 27
    Last Post: 09-11-2009, 10:15 AM
  3. performance is degraded in 1.6
    By jagadeeshchinni in forum New To Java
    Replies: 1
    Last Post: 10-22-2008, 04:24 PM
  4. Performance issue
    By mathes_n in forum Web Frameworks
    Replies: 8
    Last Post: 09-02-2008, 05:11 AM
  5. performance problem on osx
    By coldnebo in forum Advanced Java
    Replies: 3
    Last Post: 08-01-2008, 09:39 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
  •