Advertisement
Guest User

Untitled

a guest
Feb 27th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.35 KB | None | 0 0
  1. import java.text.DateFormat;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4.  
  5. import com.mindprod.fastcat.FastCat;
  6.  
  7. public class CatTester {
  8.     private interface Tester {
  9.         public int test(final long junk);
  10.     }
  11.  
  12.     private static class TestFastCat implements Tester {
  13.         private final FastCat   sb;
  14.  
  15.         public TestFastCat(final FastCat sb) {
  16.             this.sb = sb;
  17.         }
  18.  
  19.         @Override
  20.         public int test(final long junk) {
  21.             final FastCat sbLocal = sb == null ? new FastCat() : sb;
  22.             final int start = sbLocal.length();
  23.             //          sbLocal.append("abcdef");
  24.             //          sbLocal.append(1234);
  25.             //          sbLocal.append("xvckxjcvh");
  26.             //          sbLocal.append(junk);
  27.             //          sbLocal.append("xvckxjcvh");
  28.             //          sbLocal.append(junk + 1);
  29.             sbLocal.append("abcdef", 1234, "xvckxjcvh", junk, "xvckxjcvh", junk + 1);
  30.             return sbLocal.length() - start;
  31.         }
  32.     }
  33.  
  34.     private static class TestStringBuffer implements Tester {
  35.         private final StringBuffer  sb;
  36.  
  37.         public TestStringBuffer(final StringBuffer sb) {
  38.             this.sb = sb;
  39.         }
  40.  
  41.         @Override
  42.         public int test(final long junk) {
  43.             final StringBuffer sbLocal = sb == null ? new StringBuffer() : sb;
  44.             final int start = sbLocal.length();
  45.             sbLocal.append("abcdef");
  46.             sbLocal.append(1234);
  47.             sbLocal.append("xvckxjcvh");
  48.             sbLocal.append(junk);
  49.             sbLocal.append("xvckxjcvh");
  50.             sbLocal.append(junk + 1);
  51.             return sbLocal.length() - start;
  52.         }
  53.     }
  54.  
  55.     private static class TestStringBuilder implements Tester {
  56.         private final StringBuilder sb;
  57.  
  58.         public TestStringBuilder(final StringBuilder sb) {
  59.             this.sb = sb;
  60.         }
  61.  
  62.         @Override
  63.         public int test(final long junk) {
  64.             final StringBuilder sbLocal = sb == null ? new StringBuilder() : sb;
  65.             final int start = sbLocal.length();
  66.             sbLocal.append("abcdef");
  67.             sbLocal.append(1234);
  68.             sbLocal.append("xvckxjcvh");
  69.             sbLocal.append(junk);
  70.             sbLocal.append("xvckxjcvh");
  71.             sbLocal.append(junk + 1);
  72.             return sbLocal.length() - start;
  73.         }
  74.     }
  75.  
  76.     public static void main(final String[] args) {
  77.         new CatTester().test();
  78.     }
  79.  
  80.     private final DateFormat    DF  = new SimpleDateFormat("mm':'ss'.'SSS'ms'");
  81.  
  82.     private void loopTest(final String name, final Tester tester, final int loops) {
  83.         final long start = System.currentTimeMillis();
  84.         long junk = 0;
  85.         for (int i = 1; i < loops; i++) {
  86.             junk += tester.test(junk);
  87.         }
  88.         if (name != null) {
  89.             System.out.println(String.format("Took %s to append %,d chars total with %s.", timestamp(start), junk, name));
  90.         }
  91.     }
  92.  
  93.     private void test() {
  94.         final int loops = 10000000;
  95.  
  96.         // To warm up the VM, HotSpot etc
  97.         loopTest(null, new TestStringBuilder(null), loops);
  98.         loopTest(null, new TestStringBuffer(null), loops);
  99.         loopTest(null, new TestFastCat(null), loops);
  100.  
  101.         System.out.println("Fresh SB/FCs:");
  102.         loopTest("FastCat", new TestFastCat(null), loops);
  103.         loopTest("StringBuffer", new TestStringBuffer(null), loops);
  104.         loopTest("StringBuilder", new TestStringBuilder(null), loops);
  105.  
  106.         System.out.println("Reused SB/FCs:");
  107.         loopTest("FastCat", new TestFastCat(new FastCat(loops * 20)), loops);
  108.         loopTest("StringBuffer", new TestStringBuffer(new StringBuffer(loops * 20)), loops);
  109.         loopTest("StringBuilder", new TestStringBuilder(new StringBuilder(loops * 20)), loops);
  110.     }
  111.  
  112.     private String timestamp(final long start) {
  113.         return DF.format(new Date(System.currentTimeMillis() - start));
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement