Most efficient way to concatenate strings?

后端 未结 17 1567
野趣味
野趣味 2020-11-22 03:04

What\'s the most efficient way to concatenate strings?

17条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 03:40

    Rico Mariani, the .NET Performance guru, had an article on this very subject. It's not as simple as one might suspect. The basic advice is this:

    If your pattern looks like:

    x = f1(...) + f2(...) + f3(...) + f4(...)

    that's one concat and it's zippy, StringBuilder probably won't help.

    If your pattern looks like:

    if (...) x += f1(...)
    if (...) x += f2(...)
    if (...) x += f3(...)
    if (...) x += f4(...)

    then you probably want StringBuilder.

    Yet another article to support this claim comes from Eric Lippert where he describes the optimizations performed on one line + concatenations in a detailed manner.

提交回复
热议问题