String vs. StringBuilder

后端 未结 24 1251
眼角桃花
眼角桃花 2020-11-22 06:22

I understand the difference between String and StringBuilder (StringBuilder being mutable) but is there a large performance difference

24条回答
  •  伪装坚强ぢ
    2020-11-22 06:49

    Yes, the performance difference is significant. See the KB article "How to improve string concatenation performance in Visual C#".

    I have always tried to code for clarity first, and then optimize for performance later. That's much easier than doing it the other way around! However, having seen the enormous performance difference in my applications between the two, I now think about it a little more carefully.

    Luckily, it's relatively straightforward to run performance analysis on your code to see where you're spending the time, and then to modify it to use StringBuilder where needed.

提交回复
热议问题