title: String.Join performance issue in C#
body: <p>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.  The function is to be written to optimize performance when it is called thousands of times per second.</p>
  
  <ol>
  <li><p>I know that .net has a function called <code>String.Join</code>, to which I may pass in the space character as a separator along with the original string.</p></li>
  <li><p>Barring the use of <code>String.Join</code>, I can use the <code>StringBuilder</code> class to append spaces after each character.</p></li>
  <li><p>Another way to accomplish this task is to declare a character array with 2*n-1 characters (You have to add n-1 characters for the spaces).  The character array can be filled in a loop and then passed to the String <code>constructor</code>.</p></li>
  </ol>
  
  <p>I've written some .net code that runs each of these algorithms one millions times each with the parameter <code>"Hello, World"</code> and measures how long it takes to execute.  Method (3) is much, much faster than (1) or (2).</p>
  
  <p>I know that (3) should be very fast because it avoids creating any additional string references to be garbage collected, but it seems to me that a built-in .net function such as <code>String.Join</code> should yield good performance.  Why is using <code>String.Join</code> so much slower than doing the work by hand?</p>
  
  <pre><code>public static class TestClass
  {
      // 491 milliseconds for 1 million iterations
      public static string Space1(string s) 
      {            
          return string.Join(" ", s.AsEnumerable());
      }
  
      //190 milliseconds for 1 million iterations
      public static string Space2(string s) 
      {
          if (s.Length &lt; 2)
              return s;
          StringBuilder sb = new StringBuilder();
          sb.Append(s[0]);
          for (int i = 1; i &lt; s.Length; i++)
          {
              sb.Append(' ');
              sb.Append(s[i]);
          }            
          return sb.ToString();
      }
  
      // 50 milliseconds for 1 million iterations
      public static string Space3(string s) 
      {
          if (s.Length &lt; 2)
              return s;
          char[] array = new char[s.Length * 2 - 1];
          array[0] = s[0];
          for (int i = 1; i &lt; s.Length; i++)
          {
              array[2*i-1] = ' ';
              array[2*i] = s[i];
          }
          return new string(array);
      }
  </code></pre>
  
  <p><b>Update: </b> I have changed my project to "Release" mode and updated my elapsed times in the question accordingly.</p>