C# : When exactly should a StringBuilder be used?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    #1

    C# : When exactly should a StringBuilder be used?

    Hi!

    I'm very interesting in when to use exactly the StringBuilder?

    For example for something like this?:

    String strTest1 = "This";
    String strTest2 = "Test";
    StringBuilder stbTest = new StringBuilder() ;
    stbTest.Append( strTest1). Append("is a "). Append(stbTest) ;


    can someone provide some sample codes when to use a StringBuilder?

    And can someone tell me his experience about the performance of a
    StringBuilder?


    Regards,


    gicio


  • Roy Osherove

    #2
    Re: C# : When exactly should a StringBuilder be used?

    A StringBuilder's main functionality is , as it's name implies, to build
    strings. The fact is, in .Net, if you need to build a long string (using a
    long loop for example) it would be much slower to accomplish by using
    simple concatenation than it would be using a StringBuilder.
    The speed improvement is very big. This is because internally, the
    StringBuilder uses string pointers for concatenation, while a regular
    concat operation actually creates a new string from each two concatenated
    string, so if you do "a" + "b" you actually get 3 seperate string back,
    a,c, and the result. in a loop doing this this is very slow. A
    StringBuilder does not create new string instances and so is much faster.
    When to use: Whenever you have a non trivial string that you need to build
    dynamically using a loop (when creating XML strings for example).

    Roy Osherove




    On Sun, 9 Nov 2003 19:06:31 +0100, <gicio@web.de > wrote:
    [color=blue]
    > Hi!
    >
    > I'm very interesting in when to use exactly the StringBuilder?
    >
    > For example for something like this?:
    >
    > String strTest1 = "This";
    > String strTest2 = "Test";
    > StringBuilder stbTest = new StringBuilder() ;
    > stbTest.Append( strTest1). Append("is a "). Append(stbTest) ;
    >
    >
    > can someone provide some sample codes when to use a StringBuilder?
    >
    > And can someone tell me his experience about the performance of a
    > StringBuilder?
    >
    >
    > Regards,
    >
    >
    > gicio[/color]

    Comment

    • Anders Borum

      #3
      Re: C# : When exactly should a StringBuilder be used?

      Hello!
      [color=blue]
      > When to use: Whenever you have a non trivial string that you need to build
      > dynamically using a loop (when creating XML strings for example).[/color]

      When building Xml; Try to use the specific writers (e.g. the XmlTextWriter).
      They support indented writing and helps you programatically write more
      readably (and maintainable) code.

      Otherwise the StringBuilder is a powerful class.

      --
      venlig hilsen / with regards
      anders borum
      --


      Comment

      • Nick Wienholt

        #4
        Re: When exactly should a StringBuilder be used?

        As the number of modification operations heads towards 10 and above,
        StringBuilder becomes faster. If the number of modifications is small (as
        it is in your example), String will be faster.

        Nick Wienholt, MVP
        Maximizing .NET Performance

        Sydney Deep .NET User Group www.sdnug.org

        <gicio@web.de > wrote in message news:bolvn8$1gc 1pb$1@news.hans enet.net...[color=blue]
        > Hi!
        >
        > I'm very interesting in when to use exactly the StringBuilder?
        >
        > For example for something like this?:
        >
        > String strTest1 = "This";
        > String strTest2 = "Test";
        > StringBuilder stbTest = new StringBuilder() ;
        > stbTest.Append( strTest1). Append("is a "). Append(stbTest) ;
        >
        >
        > can someone provide some sample codes when to use a StringBuilder?
        >
        > And can someone tell me his experience about the performance of a
        > StringBuilder?
        >
        >
        > Regards,
        >
        >
        > gicio
        >
        >[/color]


        Comment

        • Anders Borum

          #5
          Re: When exactly should a StringBuilder be used?

          Doesn't that depend on the size of the strings being concatenated? The
          longer the strings, the more memory to be copied ..

          --
          venlig hilsen / with regards
          anders borum
          --


          Comment

          • Jj

            #6
            Re: When exactly should a StringBuilder be used?

            StringBuilder is more efficient when you do string concatenating, especially
            when you do that a lot vs. using += operator.

            Jianjun

            <gicio@web.de > wrote in message news:bolvn8$1gc 1pb$1@news.hans enet.net...[color=blue]
            > Hi!
            >
            > I'm very interesting in when to use exactly the StringBuilder?
            >
            > For example for something like this?:
            >
            > String strTest1 = "This";
            > String strTest2 = "Test";
            > StringBuilder stbTest = new StringBuilder() ;
            > stbTest.Append( strTest1). Append("is a "). Append(stbTest) ;
            >
            >
            > can someone provide some sample codes when to use a StringBuilder?
            >
            > And can someone tell me his experience about the performance of a
            > StringBuilder?
            >
            >
            > Regards,
            >
            >
            > gicio
            >
            >[/color]


            Comment

            • Nick Wienholt

              #7
              Re: When exactly should a StringBuilder be used?

              > Doesn't that depend on the size of the strings being concatenated? The[color=blue]
              > longer the strings, the more memory to be copied ..[/color]

              Yep - that's why there is no precise number of operations where it can be
              said StringBuilder is better. Somewhere between 3 and 8 is what I have seen
              in benchmarks, so my general recommendation is once you're at 10,
              StringBuilder is definitely the way to go. The performance difference
              between the two options is not dramatic till you start doing lots of
              operations, so there is no point worrying about whether 4 or 7 is the
              optimum number for a particular case.

              Nick


              Comment

              • Klaus H. Probst

                #8
                Re: When exactly should a StringBuilder be used?

                Aside from the other answers you've received, sometimes depending on what
                you're doing it's actually better to use a memory stream. In some cases
                you'll get 50-200% speed improvements. But we're talking *large* strings =)


                --
                _______________ _____
                Klaus H. Probst, MVP
                http://www.vbbox.com/


                <gicio@web.de > wrote in message news:bolvn8$1gc 1pb$1@news.hans enet.net...[color=blue]
                > Hi!
                >
                > I'm very interesting in when to use exactly the StringBuilder?
                >
                > For example for something like this?:
                >
                > String strTest1 = "This";
                > String strTest2 = "Test";
                > StringBuilder stbTest = new StringBuilder() ;
                > stbTest.Append( strTest1). Append("is a "). Append(stbTest) ;
                >
                >
                > can someone provide some sample codes when to use a StringBuilder?
                >
                > And can someone tell me his experience about the performance of a
                > StringBuilder?
                >
                >
                > Regards,
                >
                >
                > gicio
                >
                >[/color]


                Comment

                • Russ Bishop

                  #9
                  Re: When exactly should a StringBuilder be used?

                  What about doing lots of small operations? One could incur the overhead of
                  creating a stringbuilder, then clear it after each small operation (say
                  contat of 5 strings.) Ala:

                  sb.append(str1)
                  sb.append(str2)
                  (etc)
                  response.write( sb.tostring)
                  sb.remove(0, sb.length)

                  sb.append(newst r1)
                  sb.append(newst r2)

                  etc.


                  Thoughts?

                  -- russ


                  "Nick Wienholt" <goyousharks@ho tmail.com> wrote in message
                  news:e6dollwpDH A.2940@TK2MSFTN GP09.phx.gbl...[color=blue]
                  > As the number of modification operations heads towards 10 and above,
                  > StringBuilder becomes faster. If the number of modifications is small (as
                  > it is in your example), String will be faster.
                  >
                  > Nick Wienholt, MVP
                  > Maximizing .NET Performance
                  > http://www.apress.com/book/bookDisplay.html?bID=217
                  > Sydney Deep .NET User Group www.sdnug.org
                  >
                  > <gicio@web.de > wrote in message news:bolvn8$1gc 1pb$1@news.hans enet.net...[color=green]
                  > > Hi!
                  > >
                  > > I'm very interesting in when to use exactly the StringBuilder?
                  > >
                  > > For example for something like this?:
                  > >
                  > > String strTest1 = "This";
                  > > String strTest2 = "Test";
                  > > StringBuilder stbTest = new StringBuilder() ;
                  > > stbTest.Append( strTest1). Append("is a "). Append(stbTest) ;
                  > >
                  > >
                  > > can someone provide some sample codes when to use a StringBuilder?
                  > >
                  > > And can someone tell me his experience about the performance of a
                  > > StringBuilder?
                  > >
                  > >
                  > > Regards,
                  > >
                  > >
                  > > gicio
                  > >
                  > >[/color]
                  >
                  >[/color]


                  Comment

                  Working...