Azathoth42

JavaSpeedTest

Mar 1st, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 KB | None | 0 0
  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3.  
  4. public class StringBuilderTest {
  5.  
  6.     public static final void main(String[] args) throws Exception {
  7.         String str = "abcdefgh"+"efghefgh";
  8.         int imax = 1024 / str.length() * 1024 * 4;
  9.  
  10.         long time = System.currentTimeMillis();
  11.         System.out.println("exec.tm.sec\tstr.length\tallocated memory:free memory:memory used");
  12.         Runtime runtime = Runtime.getRuntime();
  13.         System.out.println("0\t\t0\t\t"+runtime.totalMemory()/1024 +":"+ runtime.freeMemory()/1024+":"+(runtime.totalMemory()-runtime.freeMemory())/1024);
  14.  
  15.         StringBuilder gstrBuilder = new StringBuilder("");
  16.         int i=0;
  17.         int lngth;
  18.  
  19.         Pattern p = Pattern.compile("efgh");
  20.         Matcher m;
  21.         while (i++ < imax+1000) {
  22.             gstrBuilder.append(str);
  23.             m = p.matcher(gstrBuilder);
  24.             gstrBuilder = new StringBuilder(m.replaceAll("____"));
  25.             lngth=str.length()*i;
  26.             if ((lngth % (1024*256)) == 0) {
  27.                 System.out.println(((System.currentTimeMillis()-time)/1000)+"sec\t\t"+lngth/1024+"kb\t\t"+runtime.totalMemory()/1024+":"+runtime.freeMemory()/1024+":"+(runtime.totalMemory()-runtime.freeMemory())/1024);
  28.             }
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment