• (cs) in reply to
    :
    Anyone had the bright idea that this code is probably converted with some sort of automatic generator from VB or such?


    I sure hope not.  It's a really stupid automatic tool if it was.

    Kelly
  • (unregistered)

    <font style="BACKGROUND-COLOR: #efefef">Does't Java automatically use a StringBuffer when you spell things the easy way?  (E.g., a.toString() + ", " + b.toString().)

    The C# compiler uses <font size="3">

    string [mscorlib]System.String::Concat(object[])

    </font>in that case.  So, when you're building a one-off string, not in a loop, have a reasonable number of terms, don't get so tricky, just make it readable. :)</font>

  • (cs) in reply to
    :

    <font style="BACKGROUND-COLOR: #efefef">Does't Java automatically use a StringBuffer when you spell things the easy way?  (E.g., a.toString() + ", " + b.toString().)

    The C# compiler uses <font size="3">

    string [mscorlib]System.String::Concat(object[])

    </font>in that case.  So, when you're building a one-off string, not in a loop, have a reasonable number of terms, don't get so tricky, just make it readable. :)</font>



    I'm not positive that Java doesn't do this, but I'm pretty sure it doesn't.  This would be a very specific optimization, and from what I've seen in the past at least, it doesn't seem that the sun java compiler automatically does those.  I could be wrong, of course...  I just seem to remember decompiling several things in my research and finding them very poorly (actually not at all) optimized.

    That being said, this would be an easy "no-brainer" optimization and so they may be doing it in their compiler.  However, I'm very skeptical that any such thing would be the "standard" way of doing things - i.e. that all compilers would agree in their implementation of this type of construct.

    Also, since there is overhead in creating the stringbuffer object, there may be cases (i.e. 1 concat, 2 concats, 3 concats) where depending on the length of the strings it may be better to "do what the user said" rather than optimizing using StringBuffer.

    Kelly
  • (unregistered) in reply to kellyleahy

    kellyleahy:
    I'm not positive that Java doesn't do this, but I'm pretty sure it doesn't.


    Sure, make me look it up. :)  It's an allowed optimization per the 2.0 Java language spec, which doesn't speak to whether any compiler does it.  I wonder where my little morsel of questionable memory came from... the usual place, no doubt.
    ____
    <font color="#000080"><font size="1">15.18.1.2 </font><font size="1">Optimization of String Concatenation

    An implementation may choose to perform conversion and concatenation in one step to avoid creating and then discarding an intermediate String object. To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression.</font></font>

    <font color="#000080" size="1">For primitive types, an implementation may also optimize away the creation of a wrapper object by converting directly from a primitive type to a string.

    </font>
  • (cs) in reply to
    :

    [image] kellyleahy wrote:
    I'm not positive that Java doesn't do this, but I'm pretty sure it doesn't.


    Sure, make me look it up. :)



    Sorry, just couldn't bring myself to be interested enough in this to go look for myself.

    Kelly

  • (unregistered)

    Looks nice, though.

Leave a comment on “The not-so-efficient StringBuilder”

Log In or post as a guest

Replying to comment #:

« Return to Article