String.Join performance issue in C#

后端 未结 6 1517
醉梦人生
醉梦人生 2021-01-11 11:51

I\'ve been researching a question that was presented to me: How to write a function that takes a string as input and returns a string with spaces between the characters. Th

6条回答
  •  萌比男神i
    2021-01-11 12:11

    The bad performance is not coming from String.Join, but from the way you handle each character. In this case, since characters have to be handled individually, your first method will create much more intermediate strings and the second method suffers from two .Append method calls for each character. Your third method does not involve a lots of intermediate strings or methods calls and that's the reason why your third method is the fastest.

提交回复
热议问题