title: Is there a performance difference between these two algorithms for shuffling an IEnumerable?
body: <p>These two questions give similar algorthims for shuffling an IEnumerable:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/1287567/c-is-using-random-and-orderby-a-good-shuffle-algorithm">C#: Is using Random and OrderBy a good shuffle algorithm?</a></li>
  <li><a href="https://stackoverflow.com/questions/254573/can-you-enumerate-a-collection-in-c-out-of-order">Can you enumerate a collection in C# out of order?</a></li>
  </ul>
  
  <p>Here are the two methods side-by-side:</p>
  
  <pre><code>public static IEnumerable&lt;T&gt; Shuffle1&lt;T&gt; (this IEnumerable&lt;T&gt; source)
  {
      Random random = new Random ();
      T [] copy = source.ToArray ();
  
      for (int i = copy.Length - 1; i &gt;= 0; i--) {
          int index = random.Next (i + 1);
          yield return copy [index];
          copy [index] = copy [i];
      }
  }
  
  
  public static IEnumerable&lt;T&gt; Shuffle2&lt;T&gt; (this IEnumerable&lt;T&gt; source)
  {
      Random random = new Random ();
      List&lt;T&gt; copy = source.ToList ();
  
      while (copy.Count &gt; 0) {
          int index = random.Next (copy.Count);
          yield return copy [index];
          copy.RemoveAt (index);
      }
  }
  </code></pre>
  
  <p>They are basically identical, except one uses a <code>List</code>, and one uses an array. Conceptually, the second one seems more clear to me. But is there a substantial performance benefit to be gained from using an array? Even if the Big-O time is the same, if it is several times faster, it could make a noticeable difference.</p>