StringBuilder

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

    StringBuilder

    Is string builder intelligent enough to handle concats without behaving like
    string?

    Consider
    myStringBuilder .Append("one" + "two")

    what does the '+' do here? Because this syntax is also legal but what is the
    cost compared to
    myStringBuilder .Append("one");
    myStringBuilder .Append("two");

    and is there a Microsoft recommended pattern for this?


  • Greg Ewing [MVP]

    #2
    Re: StringBuilder

    Alvin, your first example, myStringBuilder .Append("one" + "two") will
    basically compile to a string concatenation and then the stringbuilder
    append.

    Overall doing an Append twice will be faster. If you are only doing it once
    though it's not a huge difference.

    --
    Greg Ewing [MVP]
    Accomplish Your Mission with Better IT | IT support for nonprofits. You deserve peace of mind. National and remote outsourced nonprofit IT.



    "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
    message news:uKmSUZhfDH A.3076@tk2msftn gp13.phx.gbl...[color=blue]
    > Is string builder intelligent enough to handle concats without behaving[/color]
    like[color=blue]
    > string?
    >
    > Consider
    > myStringBuilder .Append("one" + "two")
    >
    > what does the '+' do here? Because this syntax is also legal but what is[/color]
    the[color=blue]
    > cost compared to
    > myStringBuilder .Append("one");
    > myStringBuilder .Append("two");
    >
    > and is there a Microsoft recommended pattern for this?
    >
    >[/color]


    Comment

    • Kieran Benton

      #3
      Re: StringBuilder

      I'm sure Jon Skeet took a look at the IL for this kind of thing a while ago
      and noted the C# compiler is smart enought to make the "one" and "two"
      constants "onetwo" at compile time. Of course this wont work if your soing
      something at runtime.

      HTH
      Kieran

      "Greg Ewing [MVP]" <gewing@_NO_SPA M_gewing.com> wrote in message
      news:uv0LIlhfDH A.2984@TK2MSFTN GP11.phx.gbl...[color=blue]
      > Alvin, your first example, myStringBuilder .Append("one" + "two") will
      > basically compile to a string concatenation and then the stringbuilder
      > append.
      >
      > Overall doing an Append twice will be faster. If you are only doing it[/color]
      once[color=blue]
      > though it's not a huge difference.
      >
      > --
      > Greg Ewing [MVP]
      > http://www.citidc.com
      >
      >
      > "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
      > message news:uKmSUZhfDH A.3076@tk2msftn gp13.phx.gbl...[color=green]
      > > Is string builder intelligent enough to handle concats without behaving[/color]
      > like[color=green]
      > > string?
      > >
      > > Consider
      > > myStringBuilder .Append("one" + "two")
      > >
      > > what does the '+' do here? Because this syntax is also legal but what is[/color]
      > the[color=green]
      > > cost compared to
      > > myStringBuilder .Append("one");
      > > myStringBuilder .Append("two");
      > >
      > > and is there a Microsoft recommended pattern for this?
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Jon Skeet

        #4
        Re: StringBuilder

        Kieran Benton <kieran@teejpc. homeip.net> wrote:[color=blue]
        > I'm sure Jon Skeet took a look at the IL for this kind of thing a while ago
        > and noted the C# compiler is smart enought to make the "one" and "two"
        > constants "onetwo" at compile time. Of course this wont work if your soing
        > something at runtime.[/color]

        Indeed - and it's even specified in the C# specification, section
        14.15, which governs what counts as a constant expression and then
        states:

        <quote>
        Whenever an expression is of one of the types listed above and contains
        only the constructs listed above, the expression is evaluated at
        compile-time.
        </quote>


        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • TJoker .NET [MVP]

          #5
          Re: StringBuilder

          My two cents.
          For a smal number of string operations, the StringBuilder may even be slower
          than regular string concatenation. I don't know of any rule of thumb when
          deciding which approach to use, but usually I resort to a StringBuilder when
          I see concatenations inside a for/while loop or if massive text replacement
          is being done in a long text.


          --
          TJoker, MCSD.NET
          MVP: Paint, Notepad, Solitaire

          *************** *************** **********


          "Greg Ewing [MVP]" <gewing@_NO_SPA M_gewing.com> wrote in message
          news:uv0LIlhfDH A.2984@TK2MSFTN GP11.phx.gbl...[color=blue]
          > Alvin, your first example, myStringBuilder .Append("one" + "two") will
          > basically compile to a string concatenation and then the stringbuilder
          > append.
          >
          > Overall doing an Append twice will be faster. If you are only doing it[/color]
          once[color=blue]
          > though it's not a huge difference.
          >
          > --
          > Greg Ewing [MVP]
          > http://www.citidc.com
          >
          >
          > "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
          > message news:uKmSUZhfDH A.3076@tk2msftn gp13.phx.gbl...[color=green]
          > > Is string builder intelligent enough to handle concats without behaving[/color]
          > like[color=green]
          > > string?
          > >
          > > Consider
          > > myStringBuilder .Append("one" + "two")
          > >
          > > what does the '+' do here? Because this syntax is also legal but what is[/color]
          > the[color=green]
          > > cost compared to
          > > myStringBuilder .Append("one");
          > > myStringBuilder .Append("two");
          > >
          > > and is there a Microsoft recommended pattern for this?
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Alvin Bruney

            #6
            Re: StringBuilder

            well for starters, i got this kind of construct peppered all over my code. i
            start off with good intentions with the append but there seems to always be
            a piece of string needing to be added and not worth the starting a new line.
            i know that is so lazy. always figured the language was smart enough to know
            better.

            i didn't care to read spec 14.15. what does it say in the queens english? do
            i need to stop doing that (is really what i am asking)

            "TJoker .NET [MVP]" <nospam@nonono. no> wrote in message
            news:#nO12$ifDH A.3616@TK2MSFTN GP11.phx.gbl...[color=blue]
            > My two cents.
            > For a smal number of string operations, the StringBuilder may even be[/color]
            slower[color=blue]
            > than regular string concatenation. I don't know of any rule of thumb when
            > deciding which approach to use, but usually I resort to a StringBuilder[/color]
            when[color=blue]
            > I see concatenations inside a for/while loop or if massive text[/color]
            replacement[color=blue]
            > is being done in a long text.
            >
            >
            > --
            > TJoker, MCSD.NET
            > MVP: Paint, Notepad, Solitaire
            >
            > *************** *************** **********
            >
            >
            > "Greg Ewing [MVP]" <gewing@_NO_SPA M_gewing.com> wrote in message
            > news:uv0LIlhfDH A.2984@TK2MSFTN GP11.phx.gbl...[color=green]
            > > Alvin, your first example, myStringBuilder .Append("one" + "two") will
            > > basically compile to a string concatenation and then the stringbuilder
            > > append.
            > >
            > > Overall doing an Append twice will be faster. If you are only doing it[/color]
            > once[color=green]
            > > though it's not a huge difference.
            > >
            > > --
            > > Greg Ewing [MVP]
            > > http://www.citidc.com
            > >
            > >
            > > "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote[/color][/color]
            in[color=blue][color=green]
            > > message news:uKmSUZhfDH A.3076@tk2msftn gp13.phx.gbl...[color=darkred]
            > > > Is string builder intelligent enough to handle concats without[/color][/color][/color]
            behaving[color=blue][color=green]
            > > like[color=darkred]
            > > > string?
            > > >
            > > > Consider
            > > > myStringBuilder .Append("one" + "two")
            > > >
            > > > what does the '+' do here? Because this syntax is also legal but what[/color][/color][/color]
            is[color=blue][color=green]
            > > the[color=darkred]
            > > > cost compared to
            > > > myStringBuilder .Append("one");
            > > > myStringBuilder .Append("two");
            > > >
            > > > and is there a Microsoft recommended pattern for this?
            > > >
            > > >[/color]
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Michael Mayer

              #7
              Re: StringBuilder

              But be sure to think code readability, as well. For example, this is a lot
              more readable:

              string name = lastname + ", " + firstname;

              then the following

              StringBuilder nameBuilder = new StringBuilder() ;
              nameBuilder.App end (lastname);
              nameBuilder.App end (", ");
              nameBuilder.App end (firstname);
              string name = nameBuilder.ToS tring();

              Not to mention that the first is quicker to write and less error prone, and
              quite possibly faster in execution.

              I would even suggest that if you're concatenating 100,000 names, still only
              use the string builder to put all the names together, something like:

              StringBuilder nameBuilder = new StringBuilder() ;
              foreach (DataRow row in myDataTable)
              {
              nameBuilder.App end (row[lastname] + ", " + row[firstname] +
              Environment.New Line);
              }

              instead of:

              StringBuilder nameBuilder = new StringBuilder() ;
              foreach (DataRow row in myDataTable)
              {
              nameBuilder.App end (row[lastname]);
              nameBuilder.App end (", ");
              nameBuilder.App end (row[firstname]);
              nameBuilder.App end (Environment.Ne wLine);
              }


              I think this simplistic case would be fairly readable written either way,
              but the first example keeps the name grouped together nicely, so if you add
              an address, and other stuff, you don't need to split it all out with
              comments:

              //Add name
              four
              lines
              to build
              name

              // Add Address
              eight
              lines of
              code to
              assemble
              an address
              from street, city state, etc

              and on.

              --
              Mike Mayer

              mike@mag37.com


              "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
              message news:O88v%23Wlf DHA.2152@tk2msf tngp13.phx.gbl. ..[color=blue]
              > well for starters, i got this kind of construct peppered all over my code.[/color]
              i[color=blue]
              > start off with good intentions with the append but there seems to always[/color]
              be[color=blue]
              > a piece of string needing to be added and not worth the starting a new[/color]
              line.[color=blue]
              > i know that is so lazy. always figured the language was smart enough to[/color]
              know[color=blue]
              > better.
              >
              > i didn't care to read spec 14.15. what does it say in the queens english?[/color]
              do[color=blue]
              > i need to stop doing that (is really what i am asking)
              >
              > "TJoker .NET [MVP]" <nospam@nonono. no> wrote in message
              > news:#nO12$ifDH A.3616@TK2MSFTN GP11.phx.gbl...[color=green]
              > > My two cents.
              > > For a smal number of string operations, the StringBuilder may even be[/color]
              > slower[color=green]
              > > than regular string concatenation. I don't know of any rule of thumb[/color][/color]
              when[color=blue][color=green]
              > > deciding which approach to use, but usually I resort to a StringBuilder[/color]
              > when[color=green]
              > > I see concatenations inside a for/while loop or if massive text[/color]
              > replacement[color=green]
              > > is being done in a long text.
              > >
              > >
              > > --
              > > TJoker, MCSD.NET
              > > MVP: Paint, Notepad, Solitaire
              > >
              > > *************** *************** **********
              > >
              > >
              > > "Greg Ewing [MVP]" <gewing@_NO_SPA M_gewing.com> wrote in message
              > > news:uv0LIlhfDH A.2984@TK2MSFTN GP11.phx.gbl...[color=darkred]
              > > > Alvin, your first example, myStringBuilder .Append("one" + "two") will
              > > > basically compile to a string concatenation and then the stringbuilder
              > > > append.
              > > >
              > > > Overall doing an Append twice will be faster. If you are only doing[/color][/color][/color]
              it[color=blue][color=green]
              > > once[color=darkred]
              > > > though it's not a huge difference.
              > > >
              > > > --
              > > > Greg Ewing [MVP]
              > > > http://www.citidc.com
              > > >
              > > >
              > > > "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote[/color][/color]
              > in[color=green][color=darkred]
              > > > message news:uKmSUZhfDH A.3076@tk2msftn gp13.phx.gbl...
              > > > > Is string builder intelligent enough to handle concats without[/color][/color]
              > behaving[color=green][color=darkred]
              > > > like
              > > > > string?
              > > > >
              > > > > Consider
              > > > > myStringBuilder .Append("one" + "two")
              > > > >
              > > > > what does the '+' do here? Because this syntax is also legal but[/color][/color][/color]
              what[color=blue]
              > is[color=green][color=darkred]
              > > > the
              > > > > cost compared to
              > > > > myStringBuilder .Append("one");
              > > > > myStringBuilder .Append("two");
              > > > >
              > > > > and is there a Microsoft recommended pattern for this?
              > > > >
              > > > >
              > > >
              > > >[/color]
              > >
              > >[/color]
              >
              >[/color]


              Comment

              • Jay B. Harlow [MVP - Outlook]

                #8
                Re: StringBuilder

                Michael,
                Just remember though that:[color=blue]
                > StringBuilder nameBuilder = new StringBuilder() ;
                > foreach (DataRow row in myDataTable)
                > {
                > nameBuilder.App end (row[lastname] + ", " + row[firstname] +
                > Environment.New Line);
                > }[/color]
                For a 100,000 names you just created 100,000 string objects that will need
                to be garbage collected.

                Where as:[color=blue]
                > StringBuilder nameBuilder = new StringBuilder() ;
                > foreach (DataRow row in myDataTable)
                > {
                > nameBuilder.App end (row[lastname]);
                > nameBuilder.App end (", ");
                > nameBuilder.App end (row[firstname]);
                > nameBuilder.App end (Environment.Ne wLine);
                > }
                >[/color]
                Will not create 100,000 string objects.

                In both cases you should set the initial capacity on the StringBuilder
                constructor as the internal buffer is going to be reallocated each time it
                runs out of space. The buffer starts at 16 if nothing no value is given.
                Normally you need to make an guess an approximate how much capacity you will
                need, for example 100,000 names * (25 first name + 25 last name + 1 comma +
                1 newline) = 5,200,000.

                I normally avoid calling String.Concat (C# + or VB.NET & on strings) when
                using a StringBuilder as part of the reason I use the StringBuilder is to
                avoid any intermediate string objects on heap. Using String.Concat with
                StringBuilder is just creating those objects...

                Just a thought
                Jay

                "Michael Mayer" <mike@mag37.com > wrote in message
                news:%23ufPCImf DHA.1732@TK2MSF TNGP12.phx.gbl. ..[color=blue]
                > But be sure to think code readability, as well. For example, this is a[/color]
                lot[color=blue]
                > more readable:
                >
                > string name = lastname + ", " + firstname;
                >
                > then the following
                >
                > StringBuilder nameBuilder = new StringBuilder() ;
                > nameBuilder.App end (lastname);
                > nameBuilder.App end (", ");
                > nameBuilder.App end (firstname);
                > string name = nameBuilder.ToS tring();
                >
                > Not to mention that the first is quicker to write and less error prone,[/color]
                and[color=blue]
                > quite possibly faster in execution.
                >
                > I would even suggest that if you're concatenating 100,000 names, still[/color]
                only[color=blue]
                > use the string builder to put all the names together, something like:
                >
                > StringBuilder nameBuilder = new StringBuilder() ;
                > foreach (DataRow row in myDataTable)
                > {
                > nameBuilder.App end (row[lastname] + ", " + row[firstname] +
                > Environment.New Line);
                > }
                >
                > instead of:
                >
                > StringBuilder nameBuilder = new StringBuilder() ;
                > foreach (DataRow row in myDataTable)
                > {
                > nameBuilder.App end (row[lastname]);
                > nameBuilder.App end (", ");
                > nameBuilder.App end (row[firstname]);
                > nameBuilder.App end (Environment.Ne wLine);
                > }
                >
                >
                > I think this simplistic case would be fairly readable written either way,
                > but the first example keeps the name grouped together nicely, so if you[/color]
                add[color=blue]
                > an address, and other stuff, you don't need to split it all out with
                > comments:
                >
                > //Add name
                > four
                > lines
                > to build
                > name
                >
                > // Add Address
                > eight
                > lines of
                > code to
                > assemble
                > an address
                > from street, city state, etc
                >
                > and on.
                >
                > --
                > Mike Mayer
                > http://www.mag37.com/csharp/
                > mike@mag37.com
                >
                >
                > "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
                > message news:O88v%23Wlf DHA.2152@tk2msf tngp13.phx.gbl. ..[color=green]
                > > well for starters, i got this kind of construct peppered all over my[/color][/color]
                code.[color=blue]
                > i[color=green]
                > > start off with good intentions with the append but there seems to always[/color]
                > be[color=green]
                > > a piece of string needing to be added and not worth the starting a new[/color]
                > line.[color=green]
                > > i know that is so lazy. always figured the language was smart enough to[/color]
                > know[color=green]
                > > better.
                > >
                > > i didn't care to read spec 14.15. what does it say in the queens[/color][/color]
                english?[color=blue]
                > do[color=green]
                > > i need to stop doing that (is really what i am asking)
                > >
                > > "TJoker .NET [MVP]" <nospam@nonono. no> wrote in message
                > > news:#nO12$ifDH A.3616@TK2MSFTN GP11.phx.gbl...[color=darkred]
                > > > My two cents.
                > > > For a smal number of string operations, the StringBuilder may even be[/color]
                > > slower[color=darkred]
                > > > than regular string concatenation. I don't know of any rule of thumb[/color][/color]
                > when[color=green][color=darkred]
                > > > deciding which approach to use, but usually I resort to a[/color][/color][/color]
                StringBuilder[color=blue][color=green]
                > > when[color=darkred]
                > > > I see concatenations inside a for/while loop or if massive text[/color]
                > > replacement[color=darkred]
                > > > is being done in a long text.
                > > >
                > > >
                > > > --
                > > > TJoker, MCSD.NET
                > > > MVP: Paint, Notepad, Solitaire
                > > >
                > > > *************** *************** **********
                > > >
                > > >
                > > > "Greg Ewing [MVP]" <gewing@_NO_SPA M_gewing.com> wrote in message
                > > > news:uv0LIlhfDH A.2984@TK2MSFTN GP11.phx.gbl...
                > > > > Alvin, your first example, myStringBuilder .Append("one" + "two")[/color][/color][/color]
                will[color=blue][color=green][color=darkred]
                > > > > basically compile to a string concatenation and then the[/color][/color][/color]
                stringbuilder[color=blue][color=green][color=darkred]
                > > > > append.
                > > > >
                > > > > Overall doing an Append twice will be faster. If you are only doing[/color][/color]
                > it[color=green][color=darkred]
                > > > once
                > > > > though it's not a huge difference.
                > > > >
                > > > > --
                > > > > Greg Ewing [MVP]
                > > > > http://www.citidc.com
                > > > >
                > > > >
                > > > > "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com>[/color][/color][/color]
                wrote[color=blue][color=green]
                > > in[color=darkred]
                > > > > message news:uKmSUZhfDH A.3076@tk2msftn gp13.phx.gbl...
                > > > > > Is string builder intelligent enough to handle concats without[/color]
                > > behaving[color=darkred]
                > > > > like
                > > > > > string?
                > > > > >
                > > > > > Consider
                > > > > > myStringBuilder .Append("one" + "two")
                > > > > >
                > > > > > what does the '+' do here? Because this syntax is also legal but[/color][/color]
                > what[color=green]
                > > is[color=darkred]
                > > > > the
                > > > > > cost compared to
                > > > > > myStringBuilder .Append("one");
                > > > > > myStringBuilder .Append("two");
                > > > > >
                > > > > > and is there a Microsoft recommended pattern for this?
                > > > > >
                > > > > >
                > > > >
                > > > >
                > > >
                > > >[/color]
                > >
                > >[/color]
                >
                >[/color]


                Comment

                • Michael Mayer

                  #9
                  Re: StringBuilder

                  "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@ema il.msn.com> wrote in message
                  news:%2367W8Zmf DHA.3284@tk2msf tngp13.phx.gbl. ..[color=blue]
                  > Michael,
                  > Just remember though that:[color=green]
                  > > StringBuilder nameBuilder = new StringBuilder() ;
                  > > foreach (DataRow row in myDataTable)
                  > > {
                  > > nameBuilder.App end (row[lastname] + ", " + row[firstname] +
                  > > Environment.New Line);
                  > > }[/color]
                  > For a 100,000 names you just created 100,000 string objects that will need
                  > to be garbage collected.[/color]

                  Ok, I agree (and I guess it's worse since there's three concats in the above
                  so it would probably result in 300,000 temporary strings). I haven't really
                  ever tried to do the above for such a big number of strings - I might make
                  an example to see how bad it is. Most of the data I've used would only try
                  to concat a few dozens of records at a time, where you can get by with
                  runtime ineffeciencies.

                  The main thing I try to avoid is copying of the entire string over and over
                  (as you'd get if you did text += newStuff a whole bunch of times). But there
                  certainly are advantages to avoiding the short strings when there will be a
                  lot of them.
                  [color=blue]
                  > I normally avoid calling String.Concat (C# + or VB.NET & on strings) when
                  > using a StringBuilder as part of the reason I use the StringBuilder is to
                  > avoid any intermediate string objects on heap. Using String.Concat with
                  > StringBuilder is just creating those objects...[/color]

                  I guess I would have to agree, in retrospect, since a few hundred thousand
                  extra strings on the heap isn't optimal - when they can easily be avoided
                  altogether.


                  --
                  Mike Mayer

                  mike@mag37.com



                  Comment

                  • Jon Skeet

                    #10
                    Re: StringBuilder

                    Alvin Bruney <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote:[color=blue]
                    > well for starters, i got this kind of construct peppered all over my code. i
                    > start off with good intentions with the append but there seems to always be
                    > a piece of string needing to be added and not worth the starting a new line.
                    > i know that is so lazy. always figured the language was smart enough to know
                    > better.[/color]

                    Not sure exactly which construct you mean here, to be honest.
                    [color=blue]
                    > i didn't care to read spec 14.15. what does it say in the queens english? do
                    > i need to stop doing that (is really what i am asking)[/color]

                    It says that "literal1"+"lit eral2" is always evaluated at compile-time
                    to "literal1litera l2".

                    --
                    Jon Skeet - <skeet@pobox.co m>
                    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                    If replying to the group, please do not mail me too

                    Comment

                    • Jon Skeet

                      #11
                      Re: StringBuilder

                      Michael Mayer <mike@mag37.com > wrote:[color=blue]
                      > Ok, I agree (and I guess it's worse since there's three concats in the above
                      > so it would probably result in 300,000 temporary strings).[/color]

                      Nope, there's only 100,000, actually, although there are also 100,000
                      string arrays created briefly. The line of:

                      String x = a + ',' + b + 'x';

                      is compiled to String.Concat (new object[] {a, ',', b, 'x'});

                      --
                      Jon Skeet - <skeet@pobox.co m>
                      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                      If replying to the group, please do not mail me too

                      Comment

                      • Jay B. Harlow [MVP - Outlook]

                        #12
                        Re: StringBuilder

                        Michael,[color=blue]
                        > Ok, I agree (and I guess it's worse since there's three concats in the[/color]
                        above[color=blue]
                        > so it would probably result in 300,000 temporary strings). I haven't[/color]
                        really
                        As Jon said there are only 100,000 temporary strings. Then 100,000 temporary
                        string arrays.

                        Interesting: String.Concat is overloaded for variable number of strings & a
                        variable number of objects. When overloading for a variable number of
                        parameters (params) its common to have overloads for a fixed number such as
                        1, 2 and 3, as it avoids the temporary array to hold the parameters in these
                        cases.

                        String.Concat can concatenate up to 4 strings before it needs to use the
                        params version and create the array. However can only concatenate up to 3
                        objects before it needs to use the params version. Interesting I would
                        expect both to been 3 or both 4. Hmm...

                        Hope this helps
                        Jay

                        "Michael Mayer" <mike@mag37.com > wrote in message
                        news:elMzwumfDH A.1212@TK2MSFTN GP12.phx.gbl...[color=blue]
                        > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@ema il.msn.com> wrote in[/color]
                        message[color=blue]
                        > news:%2367W8Zmf DHA.3284@tk2msf tngp13.phx.gbl. ..[color=green]
                        > > Michael,
                        > > Just remember though that:[color=darkred]
                        > > > StringBuilder nameBuilder = new StringBuilder() ;
                        > > > foreach (DataRow row in myDataTable)
                        > > > {
                        > > > nameBuilder.App end (row[lastname] + ", " + row[firstname] +
                        > > > Environment.New Line);
                        > > > }[/color]
                        > > For a 100,000 names you just created 100,000 string objects that will[/color][/color]
                        need[color=blue][color=green]
                        > > to be garbage collected.[/color]
                        >
                        > Ok, I agree (and I guess it's worse since there's three concats in the[/color]
                        above[color=blue]
                        > so it would probably result in 300,000 temporary strings). I haven't[/color]
                        really[color=blue]
                        > ever tried to do the above for such a big number of strings - I might make
                        > an example to see how bad it is. Most of the data I've used would only try
                        > to concat a few dozens of records at a time, where you can get by with
                        > runtime ineffeciencies.
                        >
                        > The main thing I try to avoid is copying of the entire string over and[/color]
                        over[color=blue]
                        > (as you'd get if you did text += newStuff a whole bunch of times). But[/color]
                        there[color=blue]
                        > certainly are advantages to avoiding the short strings when there will be[/color]
                        a[color=blue]
                        > lot of them.
                        >[color=green]
                        > > I normally avoid calling String.Concat (C# + or VB.NET & on strings)[/color][/color]
                        when[color=blue][color=green]
                        > > using a StringBuilder as part of the reason I use the StringBuilder is[/color][/color]
                        to[color=blue][color=green]
                        > > avoid any intermediate string objects on heap. Using String.Concat with
                        > > StringBuilder is just creating those objects...[/color]
                        >
                        > I guess I would have to agree, in retrospect, since a few hundred thousand
                        > extra strings on the heap isn't optimal - when they can easily be avoided
                        > altogether.
                        >
                        >
                        > --
                        > Mike Mayer
                        > http://www.mag37.com/csharp/
                        > mike@mag37.com
                        >
                        >
                        >[/color]


                        Comment

                        • Alvin Bruney

                          #13
                          Re: StringBuilder

                          > Not sure exactly which construct you mean here, to be honest.
                          was talking about .append(blah + moreblah) as opposed to .append(blah);
                          ..append(morebl ah)
                          [color=blue]
                          > It says that "literal1"+"lit eral2" is always evaluated at compile-time
                          > to "literal1litera l2".[/color]

                          ok well that seems to be good for me then since it evaluates it at compile
                          time. but what happens at run-time? what if literal2 was a string object
                          (not interned) - as would frequently be the case. does it know what to do
                          then?

                          consider StringBuilder1. Append("my name is" + varName). Knowing this answer
                          is fundamentally important to me since I am building about 10,000 queries
                          that way and performance must always supercede anything else.

                          "Jon Skeet" <skeet@pobox.co m> wrote in message
                          news:MPG.19d4b8 f04604efcc9896f 7@news.microsof t.com...[color=blue]
                          > Alvin Bruney <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote:[color=green]
                          > > well for starters, i got this kind of construct peppered all over my[/color][/color]
                          code. i[color=blue][color=green]
                          > > start off with good intentions with the append but there seems to always[/color][/color]
                          be[color=blue][color=green]
                          > > a piece of string needing to be added and not worth the starting a new[/color][/color]
                          line.[color=blue][color=green]
                          > > i know that is so lazy. always figured the language was smart enough to[/color][/color]
                          know[color=blue][color=green]
                          > > better.[/color]
                          >
                          > Not sure exactly which construct you mean here, to be honest.
                          >[color=green]
                          > > i didn't care to read spec 14.15. what does it say in the queens[/color][/color]
                          english? do[color=blue][color=green]
                          > > i need to stop doing that (is really what i am asking)[/color]
                          >
                          > It says that "literal1"+"lit eral2" is always evaluated at compile-time
                          > to "literal1litera l2".
                          >
                          > --
                          > Jon Skeet - <skeet@pobox.co m>
                          > http://www.pobox.com/~skeet
                          > If replying to the group, please do not mail me too[/color]


                          Comment

                          • Jay B. Harlow [MVP - Outlook]

                            #14
                            Re: StringBuilder

                            Alvin,
                            See my & Jon's responses to Michael Meyer.
                            [color=blue]
                            > consider StringBuilder1. Append("my name is" + varName). Knowing this[/color]
                            answer[color=blue]
                            > is fundamentally important to me since I am building about 10,000 queries
                            > that way and performance must always supercede anything else.[/color]
                            You will have 10,000 temporary string objects on the heap that will need to
                            be collection. If you are concatenating more then 3 objects or 4 strings you
                            will have that many temporary object arrays on the heap, in addition to the
                            temporary string object.

                            Hope this helps
                            Jay

                            "Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
                            message news:uE3OmGsfDH A.3104@TK2MSFTN GP11.phx.gbl...[color=blue][color=green]
                            > > Not sure exactly which construct you mean here, to be honest.[/color]
                            > was talking about .append(blah + moreblah) as opposed to .append(blah);
                            > .append(morebla h)
                            >[color=green]
                            > > It says that "literal1"+"lit eral2" is always evaluated at compile-time
                            > > to "literal1litera l2".[/color]
                            >
                            > ok well that seems to be good for me then since it evaluates it at compile
                            > time. but what happens at run-time? what if literal2 was a string object
                            > (not interned) - as would frequently be the case. does it know what to do
                            > then?
                            >
                            > consider StringBuilder1. Append("my name is" + varName). Knowing this[/color]
                            answer[color=blue]
                            > is fundamentally important to me since I am building about 10,000 queries
                            > that way and performance must always supercede anything else.
                            >
                            > "Jon Skeet" <skeet@pobox.co m> wrote in message
                            > news:MPG.19d4b8 f04604efcc9896f 7@news.microsof t.com...[color=green]
                            > > Alvin Bruney <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote:[color=darkred]
                            > > > well for starters, i got this kind of construct peppered all over my[/color][/color]
                            > code. i[color=green][color=darkred]
                            > > > start off with good intentions with the append but there seems to[/color][/color][/color]
                            always[color=blue]
                            > be[color=green][color=darkred]
                            > > > a piece of string needing to be added and not worth the starting a new[/color][/color]
                            > line.[color=green][color=darkred]
                            > > > i know that is so lazy. always figured the language was smart enough[/color][/color][/color]
                            to[color=blue]
                            > know[color=green][color=darkred]
                            > > > better.[/color]
                            > >
                            > > Not sure exactly which construct you mean here, to be honest.
                            > >[color=darkred]
                            > > > i didn't care to read spec 14.15. what does it say in the queens[/color][/color]
                            > english? do[color=green][color=darkred]
                            > > > i need to stop doing that (is really what i am asking)[/color]
                            > >
                            > > It says that "literal1"+"lit eral2" is always evaluated at compile-time
                            > > to "literal1litera l2".
                            > >
                            > > --
                            > > Jon Skeet - <skeet@pobox.co m>
                            > > http://www.pobox.com/~skeet
                            > > If replying to the group, please do not mail me too[/color]
                            >
                            >[/color]


                            Comment

                            • mikeb

                              #15
                              Re: StringBuilder

                              Alvin Bruney wrote:[color=blue][color=green]
                              >>Not sure exactly which construct you mean here, to be honest.[/color]
                              >
                              > was talking about .append(blah + moreblah) as opposed to .append(blah);
                              > .append(morebla h)
                              >
                              >[color=green]
                              >>It says that "literal1"+"lit eral2" is always evaluated at compile-time
                              >>to "literal1litera l2".[/color]
                              >
                              >
                              > ok well that seems to be good for me then since it evaluates it at compile
                              > time. but what happens at run-time? what if literal2 was a string object
                              > (not interned) - as would frequently be the case. does it know what to do
                              > then?
                              >
                              > consider StringBuilder1. Append("my name is" + varName). Knowing this answer
                              > is fundamentally important to me since I am building about 10,000 queries
                              > that way and performance must always supercede anything else.[/color]

                              If the transient string objects that are created in calls like this are
                              an issue, there are a couple of ways to deal with the problem, while
                              keeping readability:

                              1) remember that StringBuilder.A ppend() returns the StringBuilder
                              instance. So you can make the above call like so:

                              StringBuilder1. Append( "my name is ").Append( varName);

                              whether this remains readable or not is a question of taste. It
                              does not create the transient string object, however.

                              2) write your own utility method that takes multiple strings to
                              append to a StringBuilder instance. Since StringBuilder is sealed, this
                              would likely be a static class - something like (untested code follows):

                              public class SbUtil {
                              public static StringBuilder Append( StringBuilder sb,
                              string s1,
                              string s2)
                              {
                              sb.Append( s1);
                              sb.Append( s2);
                              return( sb);
                              }
                              }

                              // ...

                              SbUtil.Append( StringBuilder1, "may name is ", varName);

                              [color=blue]
                              >
                              > "Jon Skeet" <skeet@pobox.co m> wrote in message
                              > news:MPG.19d4b8 f04604efcc9896f 7@news.microsof t.com...
                              >[color=green]
                              >>Alvin Bruney <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote:
                              >>[color=darkred]
                              >>>well for starters, i got this kind of construct peppered all over my[/color][/color]
                              >
                              > code. i
                              >[color=green][color=darkred]
                              >>>start off with good intentions with the append but there seems to always[/color][/color]
                              >
                              > be
                              >[color=green][color=darkred]
                              >>>a piece of string needing to be added and not worth the starting a new[/color][/color]
                              >
                              > line.
                              >[color=green][color=darkred]
                              >>>i know that is so lazy. always figured the language was smart enough to[/color][/color]
                              >
                              > know
                              >[color=green][color=darkred]
                              >>>better.[/color]
                              >>
                              >>Not sure exactly which construct you mean here, to be honest.
                              >>
                              >>[color=darkred]
                              >>>i didn't care to read spec 14.15. what does it say in the queens[/color][/color]
                              >
                              > english? do
                              >[color=green][color=darkred]
                              >>>i need to stop doing that (is really what i am asking)[/color]
                              >>
                              >>It says that "literal1"+"lit eral2" is always evaluated at compile-time
                              >>to "literal1litera l2".
                              >>
                              >>--
                              >>Jon Skeet - <skeet@pobox.co m>
                              >>http://www.pobox.com/~skeet
                              >>If replying to the group, please do not mail me too[/color]
                              >
                              >
                              >[/color]

                              --
                              mikeb

                              Comment

                              Working...