StringBuilder

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

    #16
    Re: StringBuilder

    mikeb wrote:
    [color=blue]
    > Alvin Bruney wrote:
    >[color=green][color=darkred]
    >>> 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=darkred]
    >>> 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][/color]

    You might also want to test StringBuilder.A ppendFormat() - it may be
    smart enough to avoid the transient string object creation (probably
    not, though).

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

    --
    mikeb

    Comment

    • Alvin Bruney

      #17
      Re: StringBuilder

      i like this append().append format.

      i'll give it a shot - it appears immediately readable to me by the way so
      readability won't be an issue.

      ordinarily, i don't suspect this sort of thing would be an everday issue but
      i am building a mountain of strings so i thought i should take a second look
      at memory allocation/deallocation.

      thanks for the vigilance all.

      "mikeb" <mailbox.google @mailnull.com> wrote in message
      news:O7c9OsvfDH A.3896@tk2msftn gp13.phx.gbl...[color=blue]
      > Alvin Bruney wrote:[color=green][color=darkred]
      > >>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=darkred]
      > >>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[/color][/color]
      compile[color=blue][color=green]
      > > 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[/color][/color]
      do[color=blue][color=green]
      > > then?
      > >
      > > consider StringBuilder1. Append("my name is" + varName). Knowing this[/color][/color]
      answer[color=blue][color=green]
      > > is fundamentally important to me since I am building about 10,000[/color][/color]
      queries[color=blue][color=green]
      > > 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=green]
      > >
      > > "Jon Skeet" <skeet@pobox.co m> wrote in message
      > > news:MPG.19d4b8 f04604efcc9896f 7@news.microsof t.com...
      > >[color=darkred]
      > >>Alvin Bruney <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote:
      > >>
      > >>>well for starters, i got this kind of construct peppered all over my[/color]
      > >
      > > code. i
      > >[color=darkred]
      > >>>start off with good intentions with the append but there seems to[/color][/color][/color]
      always[color=blue][color=green]
      > >
      > > be
      > >[color=darkred]
      > >>>a piece of string needing to be added and not worth the starting a new[/color]
      > >
      > > line.
      > >[color=darkred]
      > >>>i know that is so lazy. always figured the language was smart enough to[/color]
      > >
      > > know
      > >[color=darkred]
      > >>>better.
      > >>
      > >>Not sure exactly which construct you mean here, to be honest.
      > >>
      > >>
      > >>>i didn't care to read spec 14.15. what does it say in the queens[/color]
      > >
      > > english? do
      > >[color=darkred]
      > >>>i need to stop doing that (is really what i am asking)
      > >>
      > >>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
      >[/color]


      Comment

      Working...