Most efficient way to concatenate strings?

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

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

相关标签:
17条回答
  • 2020-11-22 03:36

    From Chinh Do - StringBuilder is not always faster:

    Rules of Thumb

    • When concatenating three dynamic string values or less, use traditional string concatenation.

    • When concatenating more than three dynamic string values, use StringBuilder.

    • When building a big string from several string literals, use either the @ string literal or the inline + operator.

    Most of the time StringBuilder is your best bet, but there are cases as shown in that post that you should at least think about each situation.

    0 讨论(0)
  • 2020-11-22 03:36

    It would depend on the code. StringBuilder is more efficient generally, but if you're only concatenating a few strings and doing it all in one line, code optimizations will likely take care of it for you. It's important to think about how the code looks too: for larger sets StringBuilder will make it easier to read, for small ones StringBuilder will just add needless clutter.

    0 讨论(0)
  • 2020-11-22 03:37

    Following may be one more alternate solution to concatenate multiple strings.

    String str1 = "sometext";
    string str2 = "some other text";
    
    string afterConcate = $"{str1}{str2}";
    

    string interpolation

    0 讨论(0)
  • 2020-11-22 03:39

    For just two strings, you definitely do not want to use StringBuilder. There is some threshold above which the StringBuilder overhead is less than the overhead of allocating multiple strings.

    So, for more that 2-3 strings, use DannySmurf's code. Otherwise, just use the + operator.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 03:40

    Here is the fastest method I've evolved over a decade for my large-scale NLP app. I have variations for IEnumerable<T> and other input types, with and without separators of different types (Char, String), but here I show the simple case of concatenating all strings in an array into a single string, with no separator. Latest version here is developed and unit-tested on C# 7 and .NET 4.7.

    There are two keys to higher performance; the first is to pre-compute the exact total size required. This step is trivial when the input is an array as shown here. For handling IEnumerable<T> instead, it is worth first gathering the strings into a temporary array for computing that total (The array is required to avoid calling ToString() more than once per element since technically, given the possibility of side-effects, doing so could change the expected semantics of a 'string join' operation).

    Next, given the total allocation size of the final string, the biggest boost in performance is gained by building the result string in-place. Doing this requires the (perhaps controversial) technique of temporarily suspending the immutability of a new String which is initially allocated full of zeros. Any such controversy aside, however...

    ...note that this is the only bulk-concatenation solution on this page which entirely avoids an extra round of allocation and copying by the String constructor.

    Complete code:

    /// <summary>
    /// Concatenate the strings in 'rg', none of which may be null, into a single String.
    /// </summary>
    public static unsafe String StringJoin(this String[] rg)
    {
        int i;
        if (rg == null || (i = rg.Length) == 0)
            return String.Empty;
    
        if (i == 1)
            return rg[0];
    
        String s, t;
        int cch = 0;
        do
            cch += rg[--i].Length;
        while (i > 0);
        if (cch == 0)
            return String.Empty;
    
        i = rg.Length;
        fixed (Char* _p = (s = new String(default(Char), cch)))
        {
            Char* pDst = _p + cch;
            do
                if ((t = rg[--i]).Length > 0)
                    fixed (Char* pSrc = t)
                        memcpy(pDst -= t.Length, pSrc, (UIntPtr)(t.Length << 1));
            while (pDst > _p);
        }
        return s;
    }
    
    [DllImport("MSVCR120_CLR0400", CallingConvention = CallingConvention.Cdecl)]
    static extern unsafe void* memcpy(void* dest, void* src, UIntPtr cb);
    

    I should mention that this code has a slight modification from what I use myself. In the original, I call the cpblk IL instruction from C# to do the actual copying. For simplicity and portability in the code here, I replaced that with P/Invoke memcpy instead, as you can see. For highest performance on x64 (but maybe not x86) you may want to use the cpblk method instead.

    0 讨论(0)
提交回复
热议问题