Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8150431

java.lang.String.replaceAll(String target, String replacement) can be improved

    Details

    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description

      A DESCRIPTION OF THE REQUEST :
       java.lang.String.replaceAll(String target,String replacement) needs improvement in execution time based on my test and suggestion

      JUSTIFICATION :
      While Testing, I found out that the execution time of java.lang.String.replaceAll(target, replacement) function can be improved by up to 80% if all 100% characters in String are to be replaced, and 40% increase in execution time if 50% characters are to be replaced.


      ---------- BEGIN SOURCE ----------
      public class Test{

      public static String replace(String source, String target, String replacement){
              String newString = "";
              boolean replace;
              for(int count = 0, targetLength = target.length(); count < source.length(); ){
                  replace = (source.substring(count, targetLength).equals(target));
                  if(replace){
                       //Instead of counting characters that are found, we can easily jump through them by adding there length
                      newString += replacement;
                      targetLength += target.length();
                      count += target.length();
                  }else{
                      //Here every character is counted. It is needed when there is no match
                      newString += source.charAt(count);
                      count++;
                      targetLength ++;
                  }
                  int x = 0;
              }
              return newString;
          }

      public static void compareTime(String source, String target, String replacement){
              //Creates java execution time of replaceAll
              long javaTime1 = System.nanoTime();
              System.out.println("java replaceAll() has replaced : " + source );
              System.out.println("with : " + source.replace(target, replacement));
              long javaTime2 = System.nanoTime();
               System.out.println("**************************************************************");
             //Creates my execution time
              long time1 = System.nanoTime();
              replace(source,target,replacement);
              long time2 = System.nanoTime();
              System.out.println("function replace() has replaced : " + source );
              System.out.println("with : " + source.replace(target, replacement));
              
              System.out.println("**************************************************************");
              
               System.out.println("Java Time : " + (javaTime2 - javaTime1));
              System.out.println("function Time: " + (time2 - time1));
          }
      public static void main (String[]args){
      Test.compareTime("Tast to chack tima axacution", "a", "e");
      }
      }
      ---------- END SOURCE ----------

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                igerasim Ivan Gerasimov
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: