String Builder efficiency

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

    #1

    String Builder efficiency

    I am using a StringBuilder like this:

    Dim sb As New StringBuilder

    sb.Append("Text field 1: {TXT1}" & VbCrLf)
    sb.Append("Text field 2: {TXT2}" & VbCrLf)
    sb.Append("Text field 3: {TXT3}" & VbCrLf)

    And then later in the code I use this:

    Dim sText1Value As String
    Dim sText2Value As String
    Dim sText3Value As String

    sb.Replace("{TX T1}", sText1Value)
    sb.Replace("{TX T2}", sText2Value)
    sb.Replace("{TX T3}", sText3Value)

    My question concerns the length of the string values in the sText1Value
    variables. What if their lengths are greater than the tokens ({TXT1},
    {TXT2}) that I inserted into the StringBuilder at the beginning?

    Will that cause an unnecessary string allocation? Should I make the
    tokens as long as the maximum string length that I think will be
    replaced? e. g. :

    sb.Append("Text field 1: {TXT1LONGTOKENL ENGTH}" & VbCrLf)

    Would the StringBuilder still have to make an extra string allocation
    in this case or would it just be able to replace the characters in
    place? Should I even be concerned about it?

    I could not examine the Replace function with ILDASM or Reflector to
    see exactly what it did.

    Thanks

  • AlexS

    #2
    Re: String Builder efficiency

    Chris,

    you can easily try this code and see the results.
    I am not sure what is your concern. Do you have any problems with
    sb.Replace?
    What do you expect to be maximum length in method call?

    As about "average joe" situation, replace 1 character with string of 20
    works.

    Allocations you can check with any good enough profiler.

    HTH
    Alex

    "Chris Dunaway" <dunawayc@gmail .com> wrote in message
    news:1118091995 .508767.39590@g 49g2000cwa.goog legroups.com...[color=blue]
    > I am using a StringBuilder like this:
    >
    > Dim sb As New StringBuilder
    >
    > sb.Append("Text field 1: {TXT1}" & VbCrLf)
    > sb.Append("Text field 2: {TXT2}" & VbCrLf)
    > sb.Append("Text field 3: {TXT3}" & VbCrLf)
    >
    > And then later in the code I use this:
    >
    > Dim sText1Value As String
    > Dim sText2Value As String
    > Dim sText3Value As String
    >
    > sb.Replace("{TX T1}", sText1Value)
    > sb.Replace("{TX T2}", sText2Value)
    > sb.Replace("{TX T3}", sText3Value)
    >
    > My question concerns the length of the string values in the sText1Value
    > variables. What if their lengths are greater than the tokens ({TXT1},
    > {TXT2}) that I inserted into the StringBuilder at the beginning?
    >
    > Will that cause an unnecessary string allocation? Should I make the
    > tokens as long as the maximum string length that I think will be
    > replaced? e. g. :
    >
    > sb.Append("Text field 1: {TXT1LONGTOKENL ENGTH}" & VbCrLf)
    >
    > Would the StringBuilder still have to make an extra string allocation
    > in this case or would it just be able to replace the characters in
    > place? Should I even be concerned about it?
    >
    > I could not examine the Replace function with ILDASM or Reflector to
    > see exactly what it did.
    >
    > Thanks
    >[/color]


    Comment

    • Armin Zingler

      #3
      Re: String Builder efficiency

      "Chris Dunaway" <dunawayc@gmail .com> schrieb[color=blue]
      >I am using a StringBuilder like this:
      >
      > Dim sb As New StringBuilder
      >
      > sb.Append("Text field 1: {TXT1}" & VbCrLf)
      > sb.Append("Text field 2: {TXT2}" & VbCrLf)
      > sb.Append("Text field 3: {TXT3}" & VbCrLf)
      >
      > And then later in the code I use this:
      >
      > Dim sText1Value As String
      > Dim sText2Value As String
      > Dim sText3Value As String
      >
      > sb.Replace("{TX T1}", sText1Value)
      > sb.Replace("{TX T2}", sText2Value)
      > sb.Replace("{TX T3}", sText3Value)
      >
      > My question concerns the length of the string values in the
      > sText1Value variables. What if their lengths are greater than the
      > tokens ({TXT1}, {TXT2}) that I inserted into the StringBuilder at the
      > beginning?
      >
      > Will that cause an unnecessary string allocation? Should I make
      > the tokens as long as the maximum string length that I think will
      > be replaced? e. g. :
      >
      > sb.Append("Text field 1: {TXT1LONGTOKENL ENGTH}" & VbCrLf)
      >
      > Would the StringBuilder still have to make an extra string
      > allocation in this case or would it just be able to replace the
      > characters in place? Should I even be concerned about it?
      >
      > I could not examine the Replace function with ILDASM or Reflector
      > to see exactly what it did.
      >
      > Thanks[/color]


      I think there is no big difference between adding new chars/strings to the
      stringbuilder and replacing parts by longer strings. In both cases it is
      possible that the current capacity is exceeded. In general, the initial
      capcity I choose for the stringbuilder is about the size I expect the length
      will be - unless I have no clue about the size, but this depends on the
      case.

      BTW, you should use

      sb.Append("Text field 1: {TXT1}")
      sb.Append(vbCrL f)

      instead to avoid unncessary string concatenations.


      Armin

      Comment

      • Oenone

        #4
        Re: String Builder efficiency

        Armin Zingler wrote:[color=blue]
        > BTW, you should use
        >
        > sb.Append("Text field 1: {TXT1}")
        > sb.Append(vbCrL f)
        >
        > instead to avoid unncessary string concatenations.[/color]

        Surely the compiler would take care of that particular concatenation at
        compile time and reduce it to a single string? If there were variables
        involved then sure, that would be more efficient as multiple .Append calls.
        But when concatenating multiple string literals, I'm sure the compiler
        should put these together as a single string in the output code..?

        --

        (O)enone


        Comment

        • Armin Zingler

          #5
          Re: String Builder efficiency

          "Oenone" <oenone@nowhere .com> schrieb[color=blue]
          > Armin Zingler wrote:[color=green]
          >> BTW, you should use
          >>
          >> sb.Append("Text field 1: {TXT1}")
          >> sb.Append(vbCrL f)
          >>
          >> instead to avoid unncessary string concatenations.[/color]
          >
          > Surely the compiler would take care of that particular concatenation
          > at compile time and reduce it to a single string? If there were
          > variables involved then sure, that would be more efficient as
          > multiple .Append calls. But when concatenating multiple string
          > literals, I'm sure the compiler should put these together as a
          > single string in the output code..?[/color]


          You're right, the compiler recognizes this. I didn't rely on it. ;-)

          Armin

          Comment

          • Cor Ligthert

            #6
            Re: String Builder efficiency

            Armin,
            [color=blue]
            >
            > You're right, the compiler recognizes this. I didn't rely on it. ;-)
            >[/color]

            Because you was a while not active in this newsgroup. Has been a long and
            deep discussed subject.

            :-))

            Cor


            Comment

            • Armin Zingler

              #7
              Re: String Builder efficiency

              "Cor Ligthert" <notmyfirstname @planet.nl> schrieb[color=blue]
              > Armin,
              >[color=green]
              >>
              >> You're right, the compiler recognizes this. I didn't rely on it.
              >> ;-)
              >>[/color]
              >
              > Because you was a while not active in this newsgroup. Has been a long
              > and deep discussed subject.
              >
              > :-))
              >
              > Cor[/color]

              Ah, ok, I didn't know this. :-)

              Armin

              Comment

              Working...