Advertisement
Guest User

strip bench

a guest
Aug 23rd, 2011
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class Main {
  4.  
  5.     static interface StripAlg {
  6.         String strip(String old);
  7.         String name();
  8.     }
  9.  
  10.     private final static StripAlg strip1 = new StripAlg() {
  11.         @Override
  12.         public String strip(final String old) {
  13.             final int length = old.length();
  14.             final char[] oldChars = new char[length];
  15.             old.getChars(0, length, oldChars, 0);
  16.             int newLen = 0;
  17.             for(int j = 0; j < length; j++) {
  18.                 final char ch = oldChars[j];
  19.                 if (ch >= ' ') {
  20.                     oldChars[newLen] = ch;
  21.                     newLen++;
  22.                 }
  23.             }
  24.             return new String(oldChars, 0, newLen);
  25.         }
  26.  
  27.         @Override
  28.         public String name() {
  29.             return "Alg1";
  30.         }
  31.     };
  32.  
  33.     private final static StripAlg strip2 = new StripAlg() {
  34.         @Override
  35.         public String strip(final String old) {
  36.             final StringBuffer sb = new StringBuffer(old.length());
  37.             for(int i = 0; i < old.length(); i++) {
  38.                 final char ch = old.charAt(i);
  39.                 if (ch >= ' ') {
  40.                     sb.append(ch);
  41.                 }
  42.             }
  43.             return sb.toString();
  44.         }
  45.  
  46.         @Override
  47.         public String name() {
  48.             return "Alg2";
  49.         }
  50.     };
  51.  
  52.     private final static StripAlg strip3 = new StripAlg() {
  53.         @Override
  54.         public String strip(final String old) {
  55.             final int length = old.length();
  56.             final char[] oldChars = new char[length + 1];
  57.             old.getChars(0, length, oldChars, 0);
  58.             oldChars[length] = '\0';// avoiding explicit bound check in while
  59.             int newLen = -1;
  60.             while(oldChars[++newLen] >= ' ')
  61.                 ;// find first non-printable,
  62.             // if there are none it ends on the null char I appended
  63.             for(int j = newLen; j < length; j++) {
  64.                 final char ch = oldChars[j];
  65.                 if (ch >= ' ') {
  66.                     oldChars[newLen] = ch;// the while avoids repeated
  67.                                             // overwriting here when newLen==j
  68.                     newLen++;
  69.                 }
  70.             }
  71.             return new String(oldChars, 0, newLen);
  72.         }
  73.  
  74.         @Override
  75.         public String name() {
  76.             return "Alg3";
  77.         }
  78.     };
  79.  
  80.     private static String getTestString(final int size, final int maxCharVal, final long seed) {
  81.         final Random rand = new Random(seed);
  82.         final StringBuffer sb = new StringBuffer(size);
  83.         for(int i = 0; i < size; i++) {
  84.             sb.append((char) rand.nextInt(maxCharVal));
  85.         }
  86.         return sb.toString();
  87.     }
  88.  
  89.     private static double testAlg(final StripAlg alg, final String test, final int runs) {
  90.         final long start = System.nanoTime();
  91.         for(int i = 0; i < runs; i++) {
  92.             alg.strip(test);
  93.         }
  94.         final long end = System.nanoTime();
  95.         return (double)(end - start) / 1000000;
  96.     }
  97.  
  98.     public static void main(final String[] args) {
  99.         final long seed = 0xdeadcafe;
  100.         final int size = 200;
  101.         final int maxCharVal = 200; // 32/200 ~ percentage of non printables
  102.         final String test = getTestString(size, maxCharVal, seed);
  103.         final int nrRuns = 10000;
  104.  
  105.         final StripAlg algs[] = { strip1, strip2, strip3 };
  106.         for(final StripAlg alg : algs) {
  107.             getTime(alg, size, test, nrRuns);
  108.         }
  109.  
  110.     }
  111.  
  112.     private static void getTime(final StripAlg alg, final int size, final String test,
  113.             final int nrRuns) {
  114.         double minRun = Double.MAX_VALUE;
  115.         for(int i = 0; i < 1000; i++) {
  116.             minRun = Math.min(minRun, testAlg(alg, test, nrRuns));
  117.         }
  118.         System.out.printf("%s takes %.2fms per %d elements (%.2fops/s)%n", alg.name(), minRun, nrRuns,
  119.                 (double) nrRuns / minRun * 1000);
  120.     }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement