Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. StringBuilder vs. String

StringBuilder vs. String

Scheduled Pinned Locked Moved C#
visual-studioperformancequestion
6 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yesufollower
    wrote on last edited by
    #1

    I have an application that frequently modifies the values of certain strings that are attributes of some classes that I use in the application. These strings get modified once every 1-4 minutes during normal software operation. Would the StringBuilder class provide any performance enhancement over a String in this situation? Thanks.

    T T M L V 5 Replies Last reply
    0
    • Y yesufollower

      I have an application that frequently modifies the values of certain strings that are attributes of some classes that I use in the application. These strings get modified once every 1-4 minutes during normal software operation. Would the StringBuilder class provide any performance enhancement over a String in this situation? Thanks.

      T Offline
      T Offline
      Thomas Stockwell
      wrote on last edited by
      #2

      Have you tried testing this yourself using the stopwatch class of the System.Diagnostics namespace (should be the correct namespace)?

      Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my homepage Oracle Studios[^]

      1 Reply Last reply
      0
      • Y yesufollower

        I have an application that frequently modifies the values of certain strings that are attributes of some classes that I use in the application. These strings get modified once every 1-4 minutes during normal software operation. Would the StringBuilder class provide any performance enhancement over a String in this situation? Thanks.

        T Offline
        T Offline
        tgrt
        wrote on last edited by
        #3

        Probably not. You incur a hit at three times with a StringBuilder: initialization, ensuring capacity (internal), and calling ToString. I'm guessing you'd be calling ToString too frequently to be a real benefit. That being said, you definitely need to test it to be sure. Start with the easiest, reasonable implementation and then if the performance is an issue then examine other alternatives.

        1 Reply Last reply
        0
        • Y yesufollower

          I have an application that frequently modifies the values of certain strings that are attributes of some classes that I use in the application. These strings get modified once every 1-4 minutes during normal software operation. Would the StringBuilder class provide any performance enhancement over a String in this situation? Thanks.

          M Offline
          M Offline
          Marcus J Smith
          wrote on last edited by
          #4

          I personally have noticed a large difference in performance but then I wasnt building and displaying constantly. I was doing more batch text processing.


          Cleako

          1 Reply Last reply
          0
          • Y yesufollower

            I have an application that frequently modifies the values of certain strings that are attributes of some classes that I use in the application. These strings get modified once every 1-4 minutes during normal software operation. Would the StringBuilder class provide any performance enhancement over a String in this situation? Thanks.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Hi, StringBuilder will show much better performance than string when you need to calculate complex string expressions as in:

            string s="the alphabet consists of ";
            foreach (char c in "abc...z") s=s+c;

            Inside the loop each s=s+c; will create a new string, copy the existing characters and append one (that is a quadratic operation: when the number of iterations doubles, the cost will quadruple). With StringBuilder nothing gets copied as long as the StringBuilder's capacity is sufficient. So I would say if there are fewer than 3 to 5 operations, string will be faster; in my example StringBuilder will win hands down; and yes it helps to give it adequate initial capacity. :)

            Luc Pattyn

            1 Reply Last reply
            0
            • Y yesufollower

              I have an application that frequently modifies the values of certain strings that are attributes of some classes that I use in the application. These strings get modified once every 1-4 minutes during normal software operation. Would the StringBuilder class provide any performance enhancement over a String in this situation? Thanks.

              V Offline
              V Offline
              Vega02
              wrote on last edited by
              #6

              Just to echo the others in the forum, you really need to do testing to be sure. Hook up a performance counter to figure out if it's bottlenecking your application. That being said, it sounds like your code isn't doing enough work to warrant using StringBuilder objects. Are you modifying only a handful of strings every 1 - 4 minutes? If so, is it worth it to you to optimize away a few milliseconds out of every few minutes?

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups