String Builder & String, what's the difference ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tee

    #1

    String Builder & String, what's the difference ?

    String Builder & String, what's the difference.
    and when to use which ?


    Thanks.


  • Craig Deelsnyder

    #2
    Re: String Builder & String, what's the difference ?

    On Fri, 25 Jun 2004 10:24:00 +0800, Tee <thy@streamyx.c om> wrote:
    [color=blue]
    > String Builder & String, what's the difference.
    > and when to use which ?
    >
    >
    > Thanks.
    >
    >[/color]

    Basically String is fine for never-changing values; but if you create a
    'string-like' variable and need to change it's value (such as
    concatenating a string value together) use StringBuilder:

    http://msdn.microsoft.com/library/en...ilderclass.asp


    --
    Craig Deelsnyder
    Microsoft MVP - ASP/ASP.NET

    Comment

    • Donald E. Hurley Jr.

      #3
      Re: String Builder &amp; String, what's the difference ?

      Strings are immutable. Meaning if you try to: string1 = string1 + string2,
      it needs to create a new memory location for string1 and set the original
      location for GC.

      StringBuilder does not behave this way.

      Don

      "Tee" <thy@streamyx.c om> wrote in message
      news:#UBA2tlWEH A.3944@tk2msftn gp13.phx.gbl...[color=blue]
      > String Builder & String, what's the difference.
      > and when to use which ?
      >
      >
      > Thanks.
      >
      >[/color]


      Comment

      • Saul

        #4
        Re: String Builder &amp; String, what's the difference ?

        Tee,

        Here's my take on it :

        Use stringbuilder whenever you need to do LOTS of string manipulations. For
        quick/little things, I don't bother. So if I'm just adding a string in one
        line I would do something like this --

        strA = strB & strC

        However, I had a loop where I was doing about 1,200 concatinations. Using
        string builder reduced the time this took substantially.

        Others may feel different, but this works for me.

        -Saul


        "Tee" <thy@streamyx.c om> wrote in message
        news:%23UBA2tlW EHA.3944@tk2msf tngp13.phx.gbl. ..[color=blue]
        > String Builder & String, what's the difference.
        > and when to use which ?
        >
        >
        > Thanks.
        >
        >[/color]


        Comment

        • Saul

          #5
          Re: String Builder &amp; String, what's the difference ?

          Tee,

          Here's my take on it :

          Use stringbuilder whenever you need to do LOTS of string manipulations. For
          quick/little things, I don't bother. So if I'm just adding a string in one
          line I would do something like this --

          strA = strB & strC

          However, I had a loop where I was doing about 1,200 concatinations. Using
          string builder reduced the time this took substantially.

          Others may feel different, but this works for me.

          -Saul


          "Tee" <thy@streamyx.c om> wrote in message
          news:%23UBA2tlW EHA.3944@tk2msf tngp13.phx.gbl. ..[color=blue]
          > String Builder & String, what's the difference.
          > and when to use which ?
          >
          >
          > Thanks.
          >
          >[/color]


          Comment

          • Kevin Spencer

            #6
            Re: String Builder &amp; String, what's the difference ?

            > in a few words - stringbuilder is much faster! use it as often as you can
            instead

            Odd that Microsoft doesn't recommend it that way...

            Once a StringBuilder class is instantiated, it is indeed much faster.
            However, the instantiation of an Object is costly in itself. A String is a
            primitive, which means that unless you treat it as an object, it is
            certainly much faster to use than an Object (which is why the .Net framework
            includes primitives). In addition, when you instantiate a StringBuilder, it
            allocates the default amount of Memory needed to hold its entire buffer (the
            buffer is how it avoids re-allocating Memory). Therefore, if you are doing a
            small amount of work with a string, it may indeed more efficient NOT to use
            a StringBuilder. Consider the following:

            StringBuilder s = new StringBuilder(" Hello Mom").;
            s.Append(", I mean, Mother");
            Response.Write( s.ToString());

            string s = "Hello Mom";
            s += ", I mean, Mother";
            Response.Write( s);

            Which runs faster? Which uses the most Memory?

            --
            HTH,
            Kevin Spencer
            ..Net Developer
            Microsoft MVP
            Big things are made up
            of lots of little things.

            "ISK" <ISK@discussion s.microsoft.com > wrote in message
            news:43B1DD35-A3BE-4D3D-A3BD-79D046905B58@mi crosoft.com...[color=blue]
            > in a few words - stringbuilder is much faster! use it as often as you can[/color]
            instead[color=blue]
            > of the regular string especially if you are doing a lot of string[/color]
            manipulations.[color=blue]
            >
            > a little more in depth - a string is 'unchangeable' in the sense that once[/color]
            you've[color=blue]
            > assigned a value to it, it never changes; any modifications you make to it[/color]
            like[color=blue]
            > say adding more characters, appending a string or trimming it etc will[/color]
            create a[color=blue]
            > new string object and assign the modified value to it and discard the old[/color]
            string[color=blue]
            > object leaving it for the GC to take care of during its collection[/color]
            routines..[color=blue]
            > so a lot of string manipulation means a lot of allocation de-allocation of[/color]
            memory[color=blue]
            > which isn't very efficient..obvi ously this also means that the methods[/color]
            like[color=blue]
            > s3 = s1 & s2 are slower than you would expect..
            >
            > on the other hand, the stringbuilder creates a sort of buffer (default is[/color]
            128 chars[color=blue]
            > but u can change it with its overloaded constructor) and whenever you use[/color]
            the[color=blue]
            > 'append' method, it adds the appended character(s) or string to the[/color]
            created[color=blue]
            > buffer...thus there is no creation-destruction of objects on the fly which[/color]
            means[color=blue]
            > its much faster..
            >
            > just to give you an idea..the operation s1 = s1 & s2 is almost 100 times[/color]
            slower[color=blue]
            > than stringbuilder1 = stringbuilder1. append(s2) (assuming s1 and[/color]
            stringbuilder1[color=blue]
            > are of same lengths) even for the smallest of lengths of s1 and s2 (run[/color]
            the[color=blue]
            > operations in a loop of say like a hundred thousand to notice the[/color]
            difference)[color=blue]
            >
            > hope this helps..
            >
            > "Tee" wrote:
            >[color=green]
            > > String Builder & String, what's the difference.
            > > and when to use which ?
            > >
            > >
            > > Thanks.
            > >
            > >
            > >[/color][/color]


            Comment

            • Tom Shelton

              #7
              Re: String Builder &amp; String, what's the difference ?

              On Fri, 25 Jun 2004 13:24:21 -0400, Kevin Spencer wrote:
              [color=blue][color=green]
              >> in a few words - stringbuilder is much faster! use it as often as you can[/color]
              > instead
              >
              > Odd that Microsoft doesn't recommend it that way...
              >[/color]

              They do.

              http://msdn.microsoft.com/library/de...etperftips.asp

              see the section: Use StringBuilder for Complex String Manipulation
              [color=blue]
              > Once a StringBuilder class is instantiated, it is indeed much faster.
              > However, the instantiation of an Object is costly in itself. A String is a
              > primitive, which means that unless you treat it as an object, it is[/color]

              Not true. Strings are reference types (not "primitives ). The only reason
              a string MAY be faster to instantiate is because it doesn't allocate a
              buffer, it simply allocates a 4-byte pointer and sets it to null. Where as
              a stringbuilder will allocate an initial buffer (though the default buffer
              size is only sixteen characters).
              [color=blue]
              > certainly much faster to use than an Object (which is why the .Net framework
              > includes primitives).[/color]

              Strings are objects.
              [color=blue]
              > In addition, when you instantiate a StringBuilder, it
              > allocates the default amount of Memory needed to hold its entire buffer (the[/color]

              The initial buffer (unless specified) is only sixteen characters...
              [color=blue]
              > buffer is how it avoids re-allocating Memory). Therefore, if you are doing a
              > small amount of work with a string, it may indeed more efficient NOT to use
              > a StringBuilder. Consider the following:
              >
              > StringBuilder s = new StringBuilder(" Hello Mom").;
              > s.Append(", I mean, Mother");
              > Response.Write( s.ToString());
              >
              > string s = "Hello Mom";
              > s += ", I mean, Mother";
              > Response.Write( s);
              >
              > Which runs faster? Which uses the most Memory?[/color]

              For this example, a stringbuilder would be overkill. But, put it in a loop
              or do more then 15 or 20 appends on the string... StringBuilder will
              probably come out the winner in both speed and memory - because with a
              regular string, there are temporary string objects being created in the
              background when you concatenate.

              --
              Tom Shelton [MVP]

              Comment

              • Kevin Spencer

                #8
                Re: String Builder &amp; String, what's the difference ?

                Tom, Tom, Tom. Note the exception that I made. You stated (and I quoted, and
                now quote):
                [color=blue][color=green][color=darkred]
                > >> in a few words - stringbuilder is much faster! use it as often as you[/color][/color][/color]
                can[color=blue][color=green]
                > > instead[/color][/color]

                By your own admission, my example proves this statement to be false:
                [color=blue][color=green]
                > > Which runs faster? Which uses the most Memory?[/color]
                >
                > For this example, a stringbuilder would be overkill.[/color]

                I'm not into debate. Just trying to clear the air.

                --
                HTH,
                Kevin Spencer
                ..Net Developer
                Microsoft MVP
                Big things are made up
                of lots of little things.

                "Tom Shelton" <tom@YOUKNOWTHE DRILLmtogden.co m> wrote in message
                news:1cajhyu2ev l1n.12pi0md7cl2 k.dlg@40tude.ne t...[color=blue]
                > On Fri, 25 Jun 2004 13:24:21 -0400, Kevin Spencer wrote:
                >[color=green][color=darkred]
                > >> in a few words - stringbuilder is much faster! use it as often as you[/color][/color][/color]
                can[color=blue][color=green]
                > > instead
                > >
                > > Odd that Microsoft doesn't recommend it that way...
                > >[/color]
                >
                > They do.
                >
                >[/color]
                http://msdn.microsoft.com/library/de...etperftips.asp[color=blue]
                >
                > see the section: Use StringBuilder for Complex String Manipulation
                >[color=green]
                > > Once a StringBuilder class is instantiated, it is indeed much faster.
                > > However, the instantiation of an Object is costly in itself. A String is[/color][/color]
                a[color=blue][color=green]
                > > primitive, which means that unless you treat it as an object, it is[/color]
                >
                > Not true. Strings are reference types (not "primitives ). The only reason
                > a string MAY be faster to instantiate is because it doesn't allocate a
                > buffer, it simply allocates a 4-byte pointer and sets it to null. Where[/color]
                as[color=blue]
                > a stringbuilder will allocate an initial buffer (though the default buffer
                > size is only sixteen characters).
                >[color=green]
                > > certainly much faster to use than an Object (which is why the .Net[/color][/color]
                framework[color=blue][color=green]
                > > includes primitives).[/color]
                >
                > Strings are objects.
                >[color=green]
                > > In addition, when you instantiate a StringBuilder, it
                > > allocates the default amount of Memory needed to hold its entire buffer[/color][/color]
                (the[color=blue]
                >
                > The initial buffer (unless specified) is only sixteen characters...
                >[color=green]
                > > buffer is how it avoids re-allocating Memory). Therefore, if you are[/color][/color]
                doing a[color=blue][color=green]
                > > small amount of work with a string, it may indeed more efficient NOT to[/color][/color]
                use[color=blue][color=green]
                > > a StringBuilder. Consider the following:
                > >
                > > StringBuilder s = new StringBuilder(" Hello Mom").;
                > > s.Append(", I mean, Mother");
                > > Response.Write( s.ToString());
                > >
                > > string s = "Hello Mom";
                > > s += ", I mean, Mother";
                > > Response.Write( s);
                > >
                > > Which runs faster? Which uses the most Memory?[/color]
                >
                > For this example, a stringbuilder would be overkill. But, put it in a[/color]
                loop[color=blue]
                > or do more then 15 or 20 appends on the string... StringBuilder will
                > probably come out the winner in both speed and memory - because with a
                > regular string, there are temporary string objects being created in the
                > background when you concatenate.
                >
                > --
                > Tom Shelton [MVP][/color]


                Comment

                • Cor Ligthert

                  #9
                  Re: String Builder &amp; String, what's the difference ?

                  Hi Tom,

                  I was always using
                  dim sb as new stringbuilder
                  sb.append("Hell o")
                  sb.append(" how ")
                  sb.append("are you?")

                  Than Fergus ask me once, why you do that, this is overkill? I tested it,
                  and the difference was almost nothing, and therefore I stopped to show it
                  this way forever (with long strings I always show/do it like above) and did
                  it with shortstrings as above no more. Then some weeks ago, Jay B stated to
                  me, that it was faster to do it as above. (You understand it probably, the
                  fire was on).

                  To be sure I tested it again in a very long loop, making a long string from
                  it and making this as short strings (however 100000 times or so). It is
                  always faster than concatenating the string, so I use this above again.

                  :-)

                  Cor


                  Comment

                  • Jay B. Harlow [MVP - Outlook]

                    #10
                    Re: String Builder &amp; String, what's the difference ?

                    Cor,[color=blue]
                    > it with shortstrings as above no more. Then some weeks ago, Jay B stated[/color]
                    to[color=blue]
                    > me, that it was faster to do it as above. (You understand it probably, the
                    > fire was on).[/color]
                    Please do not take things I say out of context!

                    I'm not sure which thread you think I told you that, but its obvious to me
                    you are mis-quoting me!

                    Thanks
                    Jay

                    "Cor Ligthert" <notfirstname@p lanet.nl> wrote in message
                    news:uBR8oYRXEH A.2844@TK2MSFTN GP11.phx.gbl...[color=blue]
                    > Hi Tom,
                    >
                    > I was always using
                    > dim sb as new stringbuilder
                    > sb.append("Hell o")
                    > sb.append(" how ")
                    > sb.append("are you?")
                    >
                    > Than Fergus ask me once, why you do that, this is overkill? I tested it,
                    > and the difference was almost nothing, and therefore I stopped to show it
                    > this way forever (with long strings I always show/do it like above) and[/color]
                    did[color=blue]
                    > it with shortstrings as above no more. Then some weeks ago, Jay B stated[/color]
                    to[color=blue]
                    > me, that it was faster to do it as above. (You understand it probably, the
                    > fire was on).
                    >
                    > To be sure I tested it again in a very long loop, making a long string[/color]
                    from[color=blue]
                    > it and making this as short strings (however 100000 times or so). It is
                    > always faster than concatenating the string, so I use this above again.
                    >
                    > :-)
                    >
                    > Cor
                    >
                    >[/color]


                    Comment

                    • Cor Ligthert

                      #11
                      Re: String Builder &amp; String, what's the difference ?

                      Hi Jay,
                      [color=blue]
                      >Please do not take things I say out of context![/color]
                      [color=blue]
                      >I'm not sure which thread you think I told you that, but its obvious to me
                      >you are mis-quoting me![/color]

                      Who wrote this message that was added to a message for an OP what I had
                      specially kept simple for the OP. However Jay B Harlow had again to show
                      that I was wrong and putted it in a way if I did not know that there where
                      better ways.



                      I hope this helps?

                      Cor


                      Comment

                      • Jay B. Harlow [MVP - Outlook]

                        #12
                        Re: String Builder &amp; String, what's the difference ?

                        Cor,
                        The article that I referenced in that messages tells you when you would use
                        a StringBuilder and multiple appends and tells you when you would use String
                        concatenation.

                        You need to read the article itself, to know when to use which.

                        Hope this helps
                        Jay

                        "Cor Ligthert" <notfirstname@p lanet.nl> wrote in message
                        news:e0E$t9RXEH A.500@TK2MSFTNG P09.phx.gbl...[color=blue]
                        > Hi Jay,
                        >[color=green]
                        > >Please do not take things I say out of context![/color]
                        >[color=green]
                        > >I'm not sure which thread you think I told you that, but its obvious to[/color][/color]
                        me[color=blue][color=green]
                        > >you are mis-quoting me![/color]
                        >
                        > Who wrote this message that was added to a message for an OP what I had
                        > specially kept simple for the OP. However Jay B Harlow had again to show
                        > that I was wrong and putted it in a way if I did not know that there where
                        > better ways.
                        >
                        > http://www.google.com/groups?selm=e9...TNGP10.phx.gbl
                        >
                        > I hope this helps?
                        >
                        > Cor
                        >
                        >[/color]


                        Comment

                        • Cor Ligthert

                          #13
                          Re: String Builder &amp; String, what's the difference ?

                          Jay,
                          [color=blue]
                          > The article that I referenced in that messages tells you when you would[/color]
                          use[color=blue]
                          > a StringBuilder and multiple appends and tells you when you would use[/color]
                          String[color=blue]
                          > concatenation.
                          >
                          > You need to read the article itself, to know when to use which.[/color]
                          Again, you give an answer if you are the only one who knows this or someone
                          who wrote a book. You always pass the fact that there can be in books
                          errors, which are edited in the next edition and therefore not forever true.
                          When somebody else give you an answer in the same way as you do to show you
                          this behaviour, you only become angry, so I do not do that anymore, it is
                          without any sense.

                          Did you read this conclusion about the article especially about the
                          stringbuilder?

                          Conclusion
                          The conclusion to be drawn from these test results is really very
                          straightforward . You should be using the StringBuilder class for all but the
                          most trivial string concatenation (or replace) operations. The extra effort
                          required to use the StringBuilder class is negligible and is far outweighed
                          by the potential performance and scalability benefits to be gained.

                          As I have tested and showed on this newsgroup, is that not forever for the
                          Stringbuilder replace true, however I have tested it as well for the
                          situation as stated in this thread. There are maybe situations where it
                          cannot be that way, however I think the conclusion from this article should
                          mean that using the stringbuilder instead of concatenation is preferable.

                          I hope this helps

                          Cor




                          Comment

                          • Jay B. Harlow [MVP - Outlook]

                            #14
                            Re: String Builder &amp; String, what's the difference ?

                            Cor,
                            I hope you will understand why I cannot respond to this static.

                            Jay

                            "Cor Ligthert" <notfirstname@p lanet.nl> wrote in message
                            news:uE5wRwZXEH A.556@tk2msftng p13.phx.gbl...[color=blue]
                            > Jay,
                            >[color=green]
                            > > The article that I referenced in that messages tells you when you would[/color]
                            > use[color=green]
                            > > a StringBuilder and multiple appends and tells you when you would use[/color]
                            > String[color=green]
                            > > concatenation.
                            > >
                            > > You need to read the article itself, to know when to use which.[/color]
                            > Again, you give an answer if you are the only one who knows this or[/color]
                            someone[color=blue]
                            > who wrote a book. You always pass the fact that there can be in books
                            > errors, which are edited in the next edition and therefore not forever[/color]
                            true.[color=blue]
                            > When somebody else give you an answer in the same way as you do to show[/color]
                            you[color=blue]
                            > this behaviour, you only become angry, so I do not do that anymore, it is
                            > without any sense.
                            >
                            > Did you read this conclusion about the article especially about the
                            > stringbuilder?
                            >
                            > Conclusion
                            > The conclusion to be drawn from these test results is really very
                            > straightforward . You should be using the StringBuilder class for all but[/color]
                            the[color=blue]
                            > most trivial string concatenation (or replace) operations. The extra[/color]
                            effort[color=blue]
                            > required to use the StringBuilder class is negligible and is far[/color]
                            outweighed[color=blue]
                            > by the potential performance and scalability benefits to be gained.
                            >
                            > As I have tested and showed on this newsgroup, is that not forever for the
                            > Stringbuilder replace true, however I have tested it as well for the
                            > situation as stated in this thread. There are maybe situations where it
                            > cannot be that way, however I think the conclusion from this article[/color]
                            should[color=blue]
                            > mean that using the stringbuilder instead of concatenation is preferable.
                            >
                            > I hope this helps
                            >
                            > Cor
                            >
                            >
                            >
                            >[/color]


                            Comment

                            Working...