Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import com.mindprod.fastcat.FastCat;
- public class CatTester {
- private interface Tester {
- public int test(final long junk);
- }
- private static class TestFastCat implements Tester {
- private final FastCat sb;
- public TestFastCat(final FastCat sb) {
- this.sb = sb;
- }
- @Override
- public int test(final long junk) {
- final FastCat sbLocal = sb == null ? new FastCat() : sb;
- final int start = sbLocal.length();
- // sbLocal.append("abcdef");
- // sbLocal.append(1234);
- // sbLocal.append("xvckxjcvh");
- // sbLocal.append(junk);
- // sbLocal.append("xvckxjcvh");
- // sbLocal.append(junk + 1);
- sbLocal.append("abcdef", 1234, "xvckxjcvh", junk, "xvckxjcvh", junk + 1);
- return sbLocal.length() - start;
- }
- }
- private static class TestStringBuffer implements Tester {
- private final StringBuffer sb;
- public TestStringBuffer(final StringBuffer sb) {
- this.sb = sb;
- }
- @Override
- public int test(final long junk) {
- final StringBuffer sbLocal = sb == null ? new StringBuffer() : sb;
- final int start = sbLocal.length();
- sbLocal.append("abcdef");
- sbLocal.append(1234);
- sbLocal.append("xvckxjcvh");
- sbLocal.append(junk);
- sbLocal.append("xvckxjcvh");
- sbLocal.append(junk + 1);
- return sbLocal.length() - start;
- }
- }
- private static class TestStringBuilder implements Tester {
- private final StringBuilder sb;
- public TestStringBuilder(final StringBuilder sb) {
- this.sb = sb;
- }
- @Override
- public int test(final long junk) {
- final StringBuilder sbLocal = sb == null ? new StringBuilder() : sb;
- final int start = sbLocal.length();
- sbLocal.append("abcdef");
- sbLocal.append(1234);
- sbLocal.append("xvckxjcvh");
- sbLocal.append(junk);
- sbLocal.append("xvckxjcvh");
- sbLocal.append(junk + 1);
- return sbLocal.length() - start;
- }
- }
- public static void main(final String[] args) {
- new CatTester().test();
- }
- private final DateFormat DF = new SimpleDateFormat("mm':'ss'.'SSS'ms'");
- private void loopTest(final String name, final Tester tester, final int loops) {
- final long start = System.currentTimeMillis();
- long junk = 0;
- for (int i = 1; i < loops; i++) {
- junk += tester.test(junk);
- }
- if (name != null) {
- System.out.println(String.format("Took %s to append %,d chars total with %s.", timestamp(start), junk, name));
- }
- }
- private void test() {
- final int loops = 10000000;
- // To warm up the VM, HotSpot etc
- loopTest(null, new TestStringBuilder(null), loops);
- loopTest(null, new TestStringBuffer(null), loops);
- loopTest(null, new TestFastCat(null), loops);
- System.out.println("Fresh SB/FCs:");
- loopTest("FastCat", new TestFastCat(null), loops);
- loopTest("StringBuffer", new TestStringBuffer(null), loops);
- loopTest("StringBuilder", new TestStringBuilder(null), loops);
- System.out.println("Reused SB/FCs:");
- loopTest("FastCat", new TestFastCat(new FastCat(loops * 20)), loops);
- loopTest("StringBuffer", new TestStringBuffer(new StringBuffer(loops * 20)), loops);
- loopTest("StringBuilder", new TestStringBuilder(new StringBuilder(loops * 20)), loops);
- }
- private String timestamp(final long start) {
- return DF.format(new Date(System.currentTimeMillis() - start));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement