tag:blogger.com,1999:blog-8151663570574264569.post8664203976933326460..comments2019-09-16T09:29:24.158-07:00Comments on Reducing Complexity: Strings and performanceEdinhttp://www.blogger.com/profile/01552119735073632831noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-8151663570574264569.post-77390449725898662042009-08-06T10:12:22.311-07:002009-08-06T10:12:22.311-07:00Well, I didn't want to blow my own trumpet, bu...Well, I didn't want to blow my own trumpet, but I actually did some tests a few years ago in .Net1.1.<br /><br />I wrote the results as a Codeproject article: http://www.codeproject.com/KB/cs/stringperf.aspx<br /><br />You could try and see if things have improved in more recent versions of .Net (I haven't got around to it yet).<br /><br />My general rule of thumb is: if I don't know exactly how many iterations are going to be required, I use StringBuilder; if I do know how many iterations are required and it's a small(ish) number then I'll just use string +=<br /><br />HerbieDr Herbiehttps://www.blogger.com/profile/12979943296961117525noreply@blogger.comtag:blogger.com,1999:blog-8151663570574264569.post-20902548133273686552009-08-06T06:42:13.304-07:002009-08-06T06:42:13.304-07:00Well... I am not very sure if Herbie is correct he...Well... I am not very sure if Herbie is correct here. <br /><br />To test out if his statement is correct you should run about 1000 tests in loop. The body of loop makes string concatenation 10 times. Otherwise you have too much "noise" from other programs. But I think it is worth to try. :)Gunnarhttps://www.blogger.com/profile/05924926435273401271noreply@blogger.comtag:blogger.com,1999:blog-8151663570574264569.post-6758439260114050882009-08-06T02:54:26.558-07:002009-08-06T02:54:26.558-07:00Yes, you are probably right for smaller number of ...Yes, you are probably right for smaller number of iterations, but StringBuilder does scale better. Sometimes it's not possible to know how big collections are going to be. Imagine an online business that has a separate application which goes through XML files provided by their business partners, extracts data from the files and does some intensive string concatenations. Length of XML files can vary, some partners can provide files with ten of thousands items. I had such situations in mind when writing the post. Thanks for comment:)Edinhttps://www.blogger.com/profile/01552119735073632831noreply@blogger.comtag:blogger.com,1999:blog-8151663570574264569.post-62738006138573667672009-08-05T23:47:39.755-07:002009-08-05T23:47:39.755-07:00Your loop is rather large. You should try comparin...Your loop is rather large. You should try comparing the differences for smaller numbers of concatenations, you might be surprised to find that for a lot of more realistic numbers of iterations (like tens of concatenations, instead of 10 of thousands of concatenations) StringBuilder is slower.<br /><br />HerbieDr Herbiehttps://www.blogger.com/profile/12979943296961117525noreply@blogger.com