StringBuilder vs. String
-
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.
-
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.
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[^]
-
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.
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.
-
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.
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
-
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.
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
-
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.
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?