Best way to convert IEnumerable to string?

后端 未结 6 2079
自闭症患者
自闭症患者 2020-12-10 00:34

Why isn\'t it possible to use fluent language on string?

For example:

var x = \"asdf1234\";
var y = new string(x.TakeWhile(char.IsLetter         


        
6条回答
  •  不知归路
    2020-12-10 00:51

    Edited for the release of .Net Core 2.1

    Repeating the test for the release of .Net Core 2.1, I get results like this

    1000000 iterations of "Concat" took 842ms.

    1000000 iterations of "new String" took 1009ms.

    1000000 iterations of "sb" took 902ms.

    In short, if you are using .Net Core 2.1 or later, Concat is king.

    See MS blog post for more details.


    I've made this the subject of another question but more and more, that is becoming a direct answer to this question.

    I've done some performance testing of 3 simple methods of converting an IEnumerable to a string, those methods are

    new string

    return new string(charSequence.ToArray());
    

    Concat

    return string.Concat(charSequence)
    

    StringBuilder

    var sb = new StringBuilder();
    foreach (var c in charSequence)
    {
        sb.Append(c);
    }
    
    return sb.ToString();
    

    In my testing, that is detailed in the linked question, for 1000000 iterations of "Some reasonably small test data" I get results like this,

    1000000 iterations of "Concat" took 1597ms.

    1000000 iterations of "new string" took 869ms.

    1000000 iterations of "StringBuilder" took 748ms.

    This suggests to me that there is not good reason to use string.Concat for this task. If you want simplicity use the new string approach and if want performance use the StringBuilder.

    I would caveat my assertion, in practice all these methods work fine, and this could all be over optimization.

提交回复
热议问题