Optimised String Concatenation?

Discussion in 'Java' started by Kamal Advani, Mar 8, 2006.

  1. Kamal Advani

    Kamal Advani Guest

    Hello,

    Sorry if this topic has been beaten to death, or if there's an FAQ
    (kindly point me to it); I'm a bit baffled by the following behaviour:

    -----------

    public void testTiming() {
    long start, stop;

    start = System.currentTimeMillis();
    for (int i=0; i < 1000; i++) {
    getStrBuff(""+new Date()+new Date()+new Date()+new Date());
    }
    stop = System.currentTimeMillis();
    System.out.println("strbuff took (in ms): " + (stop - start));

    start = System.currentTimeMillis();
    for (int i=0; i < 1000; i++) {
    getStrCat(""+new Date()+new Date()+new Date()+new Date());
    }
    stop = System.currentTimeMillis();
    System.out.println("strcat took (in ms): " + (stop - start));
    }

    private String getStrBuff(String x) {
    return new StringBuffer("x is ").append(x).toString();
    }

    private String getStrCat(String x) {
    return "x is " + x;
    }

    ---------

    // Output
    strbuff took (in ms): 1573
    strcat took (in ms): 130

    ---------

    Why does the StringBuffer version run significantly slower (consistent
    proportion across multiple runs are obtained)?

    I can understand if they're String literals concatenated together, or
    Strings whose values are well known in advance, allowing the compiler to
    optimise at compile time into a single literal for e.g.; in this case,
    dates can only be obtained at runtime - wouldn't that imply that the +
    version will create StringBuffers internally?

    Thanks.



    --
    Remove FOOBAR to email
    Kamal Advani, Mar 8, 2006
    #1
    1. Advertising

  2. Kamal Advani

    Bart Cremers Guest

    To start, no offense meant, your test sucks.

    You're not comparing anything there. You're testing how long it takes
    to construct a stringbuffer around a string from the pool against how
    long it takes to get the same thing from the pool and concatenate it.
    Turn around your test and you'll be even more amazed :)

    A better test would be:

    public static void testTiming() {
    long start, stop;

    start = System.currentTimeMillis();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < 1000; i++) {
    sb.append(new Date());
    sb.append(new Date());
    sb.append(new Date());
    sb.append(new Date());
    }

    stop = System.currentTimeMillis();
    System.out.println("strbuff took (in ms): " + (stop - start));

    start = System.currentTimeMillis();
    String x = "";
    for (int i = 0; i < 1000; i++) {
    x += new Date();
    x += new Date();
    x += new Date();
    x += new Date();
    }
    stop = System.currentTimeMillis();
    System.out.println("strcat took (in ms): " + (stop - start));
    }

    which more or less shows the difference in performance there.

    Regards,

    Bart
    Bart Cremers, Mar 8, 2006
    #2
    1. Advertising

  3. Kamal Advani

    Chris Uppal Guest

    Kamal Advani wrote:

    > strbuff took (in ms): 1573
    > strcat took (in ms): 130


    I suspect that it's nothing to do with the use of StringBuffers, but that you
    have run the test for too short a time for the JIT to have fully warmed up.
    Run your code inside another loop which calls testTiming():
    for (;;)
    {
    testTiming();
    }
    and watch what happens. I'll be interested and surprised if the effect doesn't
    vanish.

    BTW, what you are timing here will mostly be the time it takes to evaluate:
    ""+new Date()+new Date()+new Date()+new Date()
    since that will take a lot longer than the subsequent call to either
    getStrBuffer() or getStrCat().

    -- chris
    Chris Uppal, Mar 8, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. walala
    Replies:
    3
    Views:
    4,784
    walala
    Sep 18, 2003
  2. mark | r
    Replies:
    1
    Views:
    295
    Amber
    Nov 7, 2003
  3. Thuswise Webmaster
    Replies:
    0
    Views:
    799
    Thuswise Webmaster
    Jun 28, 2003
  4. Emmanuel
    Replies:
    3
    Views:
    319
    John J. Lee
    Feb 7, 2004
  5. wrinting an optimised code

    , May 17, 2005, in forum: C Programming
    Replies:
    11
    Views:
    552
    Kevin D. Quitt
    May 19, 2005
Loading...

Share This Page