Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #22596

Re: FastCat 'performance'

From Daniel Pitts <newsgroup.nospam@virtualinfinity.net>
Newsgroups comp.lang.java.programmer
Subject Re: FastCat 'performance'
References (4 earlier) <kgljeg$esp$1@news.albasani.net> <1rcs4eq420jj8.3menl13xfrpv.dlg@40tude.net> <kglnhr$nhj$1@news.albasani.net> <U8uXs.78289$KR.72653@newsfe27.iad> <32h92bxkny4k$.16dj0eh04iqld$.dlg@40tude.net>
Message-ID <EXuXs.78383$KR.72352@newsfe27.iad> (permalink)
Date 2013-02-27 13:32 -0800

Show all headers | View raw


On 2/27/13 12:58 PM, Joerg Meier wrote:
> On Wed, 27 Feb 2013 12:38:11 -0800, Daniel Pitts wrote:
>
>> I've made the Strings you append longer, and I've fixed the estimates
>> for the FastCat and the StringBu*ers constructors to be spot-on:
>
>> Fresh SB/FCs:
>> Cat> Init: 00.000ms, Loop: 00.175ms, End: 00.000ms, Total: 00.175ms.
>> Buf> Init: 00.000ms, Loop: 00.227ms, End: 00.000ms, Total: 00.227ms.
>> Bui> Init: 00.000ms, Loop: 00.236ms, End: 00.000ms, Total: 00.236ms.
>> Reused SB/FCs:
>> Cat> Init: 00.000ms, Loop: 00.023ms, End: 00.375ms, Total: 00.398ms.
>> Buf> Init: 00.101ms, Loop: 00.048ms, End: 00.130ms, Total: 00.279ms.
>> Bui> Init: 00.092ms, Loop: 00.052ms, End: 00.130ms, Total: 00.274ms.
>
> I'm curious, would you mind sharing the updated code on a pastebin
> somewhere ? Writing benchmarks is always tricky, so I'd be happy to learn
> from your changes.
>
> Liebe Gruesse,
> 		Joerg
>

Here it is in all its glory:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.mindprod.fastcat.FastCat;


class CatTester {
     public static final String LONG_STRING = 
"abcdefgabcdefgabcdefgabcdefgabc" +
             "defgabcdefgabcdefgabcdefgabcdefgabcde" +
             "defgabcdefgabcdefgabcdefgabcdefgabcde" +
             "defgabcdefgabcdefgabcdefgabcdefgabcde" +
             "defgabcdefgabcdefgabcdefgabcdefgabcde" +
             "defgabcdefgabcdefgabcdefgabcdefgabcde" +
             "defgabcdefgabcdefgabcdefgabcdefgabcde" +
             "defgabcdefgabcdefgabcdefgabcdefgabcde" +
             "defgabcdefgabcdefgabcdefgabcdefgabcde" +
             "fg"
             ;
     public static final String ANOTHER_STRING = "xvck" +
             "blahblahblahblahxjcvh" +
             "blahblahblahblahxjcvh" +
             "blahblahblahblahxjcvh" +
             "blahblahblahblahxjcvh" +
             "blahblahblahblahxjcvh" +
             "blahblahblahblahxjcvh" +
             "blahblahblahblahxjcvh" +
             "blahblahblahblahxjcvh" +
             "blahblahblahblahxjcvh" +
             "blahblahblahblahxjcvh" +
             "blahblahblahblahxjcvh" +
             "blahblahblahblahxjcvh" +
             "blahblahblahblahxjcvh" +
             "blahblahblahblahxjcvh" +
             "blahblahblahblahxjcvh" +
             "blahblahblahblahxjcvh" +
             "blahblahblahblahxjcvh" +
             "blahblahblahblahxjcvh" +
             "blahblahblahblahxjcvh" +
             "";

     private interface Tester {
		public void init();
		public int test(final long junk);
         String conclude();
     }

	private static class TestFastCat implements Tester {
		protected FastCat sb;

		public TestFastCat() {
		}

         public void init() {
         }

         public String conclude() {
             if (sb != null) {
                 return sb.toString();
             }
             return null;
         }


		public int test(final long junk) {
			final FastCat sbLocal = sb == null ? new FastCat(6) : sb;
			final int start = sbLocal.length();
             sbLocal.append(LONG_STRING);
             sbLocal.append(1234);
             sbLocal.append(ANOTHER_STRING);
             sbLocal.append(junk);
             sbLocal.append(ANOTHER_STRING);
             sbLocal.append(junk + 1);
             if (sbLocal != sb) {
                 sbLocal.toString();
             }
			return sbLocal.length() - start;
		}
	}

	private static class TestStringBuffer implements Tester {
		protected StringBuffer	sb;

		public TestStringBuffer() {
		}

         public void init() {
         }

         public int test(final long junk) {
			final StringBuffer sbLocal = sb == null ? new StringBuffer() : sb;
			final int start = sbLocal.length();
			sbLocal.append(LONG_STRING);
			sbLocal.append(1234);
			sbLocal.append(ANOTHER_STRING);
			sbLocal.append(junk);
			sbLocal.append(ANOTHER_STRING);
			sbLocal.append(junk + 1);
             if (sbLocal != sb) {
                 sbLocal.toString();
             }
			return sbLocal.length() - start;
		}

         public String conclude() {
             if (sb != null) {
                 return sb.toString();
             }
             return null;
         }

	}

	private static class TestStringBuilder implements Tester {
         protected StringBuilder	sb;

		public TestStringBuilder() {
		}


         public void init() {
         }

         public int test(final long junk) {
			final StringBuilder sbLocal = sb == null ? new StringBuilder() : sb;
			final int start = sbLocal.length();
			sbLocal.append(LONG_STRING);
			sbLocal.append(1234);
			sbLocal.append(ANOTHER_STRING);
			sbLocal.append(junk);
			sbLocal.append(ANOTHER_STRING);
			sbLocal.append(junk + 1);
             if (sbLocal != sb) {
                 sbLocal.toString();
             }
			return sbLocal.length() - start;
		}

         public String conclude() {
             if (sb != null) {
                 return sb.toString();
             }
             return null;
         }

	}

	public static void main(final String[] args) {
		new CatTester().test();
	}

	private final DateFormat	DF	= new SimpleDateFormat("ss'.'SSS'ms'");

	private long loopTest(final String name, final Tester tester, final int 
loops) {
         final long start = System.currentTimeMillis();
         tester.init();
         final long initTime = System.currentTimeMillis();
         long junk = 0;
         for (int i = 1; i < loops; i++) {
             junk += tester.test(junk);
         }
         final long appendTime = System.currentTimeMillis();
         tester.conclude();
         if (name != null) {
             System.out.println(String.format("%s> Init: %s, Loop: %s, 
End: %s, Total: %s.",
                     name,
                     timestamp(start, initTime),
                     timestamp(initTime, appendTime),
                     timestamp(appendTime),
                     timestamp(start))
                     );
		}
         return junk;
	}

	private void test() {
	    final int loops = 100000;

		// To warm up the VM, HotSpot etc
         loopTest(null, new TestFastCat(), loops);
         final long length = loopTest(null, new TestStringBuilder(), 
loops) / 10;
         loopTest(null, new TestStringBuffer(), loops);
         loopTest(null, new TestFastCatReuse(loops), loops);
         loopTest(null, new TestStringBufferReuse(length), loops);
         loopTest(null, new TestStringBuilderReuse(length), loops);
         for (int i = 0; i < 3; ++i) {
             System.out.println("Fresh SB/FCs:");
             loopTest("Cat", new TestFastCat(), loops);
             loopTest("Buf", new TestStringBuffer(), loops);
             loopTest("Bui", new TestStringBuilder(), loops);

             System.out.println("Reused SB/FCs:");
             loopTest("Cat", new TestFastCatReuse(loops), loops);
             loopTest("Buf", new TestStringBufferReuse(length), loops);
             loopTest("Bui", new TestStringBuilderReuse(length), loops);
         }
	}

     private String timestamp(final long start) {
         return timestamp(start, System.currentTimeMillis());
     }

     private String timestamp(final long start, long endTime) {
		return DF.format(new Date(endTime - start));
	}

     private static class TestFastCatReuse extends TestFastCat {
         private final int loops;

         public TestFastCatReuse(int loops) {
             this.loops = loops;
         }

         public void init() { sb = new FastCat(loops * 6); }
     }

     private static class TestStringBufferReuse extends TestStringBuffer {
         private final long length;

         public TestStringBufferReuse(long length) {
             this.length = length;
         }

         public void init() { sb = new StringBuffer((int) length);}
     }

     private static class TestStringBuilderReuse extends TestStringBuilder {
         private final long length;

         public TestStringBuilderReuse(long length) {
             this.length = length;
         }

         public void init() { sb = new StringBuilder((int) length); }
     }
}

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

simple StringBuilder proposal Roedy Green <see_website@mindprod.com.invalid> - 2013-02-26 03:53 -0800
  Re: simple StringBuilder proposal Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-02-26 15:15 +0000
    Re: simple StringBuilder proposal Roedy Green <see_website@mindprod.com.invalid> - 2013-02-27 10:25 -0800
    Re: simple StringBuilder proposal Roedy Green <see_website@mindprod.com.invalid> - 2013-02-27 10:30 -0800
      Re: simple StringBuilder proposal Andreas Leitgeb <avl@gamma.logic.tuwien.ac.at> - 2013-02-28 08:30 +0000
        Re: simple StringBuilder proposal markspace <markspace@nospam.nospam> - 2013-02-28 07:54 -0800
  Re: simple StringBuilder proposal markspace <markspace@nospam.nospam> - 2013-02-26 08:31 -0800
    Re: simple StringBuilder proposal Lew <lewbloch@gmail.com> - 2013-02-26 10:33 -0800
      Re: simple StringBuilder proposal markspace <markspace@nospam.nospam> - 2013-02-26 10:47 -0800
        Re: simple StringBuilder proposal Leif Roar Moldskred <leifm@dimnakorr.com> - 2013-02-26 13:29 -0600
          Re: simple StringBuilder proposal Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2013-02-26 12:08 -0800
        Re: simple StringBuilder proposal Lew <lewbloch@gmail.com> - 2013-02-26 13:32 -0800
          Re: simple StringBuilder proposal Roedy Green <see_website@mindprod.com.invalid> - 2013-02-27 10:33 -0800
        Re: simple StringBuilder proposal Robert Klemme <shortcutter@googlemail.com> - 2013-02-26 22:53 +0100
          Re: simple StringBuilder proposal markspace <markspace@nospam.nospam> - 2013-02-26 15:54 -0800
            Re: simple StringBuilder proposal Robert Klemme <shortcutter@googlemail.com> - 2013-03-01 07:35 +0100
      Re: simple StringBuilder proposal Roedy Green <see_website@mindprod.com.invalid> - 2013-02-27 10:31 -0800
        Re: simple StringBuilder proposal Sven Köhler <remove-sven.koehler@gmail.com> - 2013-02-28 05:14 +0100
  Re: simple StringBuilder proposal Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-02-26 09:41 -0800
  Re: simple StringBuilder proposal Jan Burse <janburse@fastmail.fm> - 2013-02-26 20:21 +0100
    Re: simple StringBuilder proposal Joerg Meier <joergmmeier@arcor.de> - 2013-02-26 23:07 +0100
      Re: simple StringBuilder proposal Jan Burse <janburse@fastmail.fm> - 2013-02-27 00:09 +0100
        Re: simple StringBuilder proposal Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-02-26 15:19 -0800
          Re: simple StringBuilder proposal Jim Janney <jjanney@shell.xmission.com> - 2013-02-27 09:34 -0700
            Re: simple StringBuilder proposal Jan Burse <janburse@fastmail.fm> - 2013-02-27 19:18 +0100
              Re: simple StringBuilder proposal Arne Vajhøj <arne@vajhoej.dk> - 2013-02-27 19:56 -0500
                Re: simple StringBuilder proposal Jan Burse <janburse@fastmail.fm> - 2013-02-28 10:19 +0100
                Re: simple StringBuilder proposal Arne Vajhøj <arne@vajhoej.dk> - 2013-02-28 08:16 -0500
                Re: simple StringBuilder proposal Jim Janney <jjanney@shell.xmission.com> - 2013-02-28 16:40 -0700
                Re: simple StringBuilder proposal Arne Vajhøj <arne@vajhoej.dk> - 2013-02-28 18:51 -0500
            Re: simple StringBuilder proposal Arne Vajhøj <arne@vajhoej.dk> - 2013-02-27 20:01 -0500
  Re: simple StringBuilder proposal Silvio <silvio@internet.com> - 2013-02-26 22:03 +0100
    Re: simple StringBuilder proposal Silvio <silvio@internet.com> - 2013-02-26 22:04 +0100
  Re: simple StringBuilder proposal Sven Köhler <remove-sven.koehler@gmail.com> - 2013-02-27 03:47 +0100
    FastCat 'performance' (was: simple StringBuilder proposal) Joerg Meier <joergmmeier@arcor.de> - 2013-02-27 18:16 +0100
      Re: FastCat 'performance' markspace <markspace@nospam.nospam> - 2013-02-27 09:58 -0800
        Re: FastCat 'performance' Jan Burse <janburse@fastmail.fm> - 2013-02-27 19:31 +0100
          Re: FastCat 'performance' Joerg Meier <joergmmeier@arcor.de> - 2013-02-27 20:00 +0100
            Re: FastCat 'performance' Jan Burse <janburse@fastmail.fm> - 2013-02-27 20:41 +0100
              Re: FastCat 'performance' Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-02-27 12:38 -0800
                Re: FastCat 'performance' Joerg Meier <joergmmeier@arcor.de> - 2013-02-27 21:58 +0100
                Re: FastCat 'performance' Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-02-27 13:32 -0800
                Re: FastCat 'performance' Lew <lewbloch@gmail.com> - 2013-02-27 13:42 -0800
                Re: FastCat 'performance' Lew <lewbloch@gmail.com> - 2013-02-27 13:44 -0800
                Re: FastCat 'performance' Joerg Meier <joergmmeier@arcor.de> - 2013-02-27 22:51 +0100
                Re: FastCat 'performance' Sven Köhler <remove-sven.koehler@gmail.com> - 2013-02-28 05:19 +0100
                Re: FastCat 'performance' Joerg Meier <joergmmeier@arcor.de> - 2013-02-28 13:09 +0100
                Re: FastCat 'performance' markspace <markspace@nospam.nospam> - 2013-02-27 13:57 -0800
                Re: FastCat 'performance' Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2013-02-27 15:23 -0800
                Re: FastCat 'performance' Arne Vajhøj <arne@vajhoej.dk> - 2013-02-27 18:52 -0500
                Re: FastCat 'performance' Sven Köhler <remove-sven.koehler@gmail.com> - 2013-03-01 17:26 +0100
      Re: FastCat 'performance' Sven Köhler <remove-sven.koehler@gmail.com> - 2013-02-27 21:00 +0100
      Re: FastCat 'performance' (was: simple StringBuilder proposal) Wanja Gayk <brixomatic@yahoo.com> - 2013-03-27 10:17 +0100

csiph-web