title: Faster alternative to nested loops?
body: <p>I have a need to create a list of combinations of numbers. The numbers are quite small so I can use <code>byte</code> rather than <code>int</code>. However it requires many nested loops in order to get every possible combination. I'm wondering if there's a more efficient manner to do what I'm after. Code so far is:</p>
  
  <pre><code>var data = new List&lt;byte[]&gt;();
  for (byte a = 0; a &lt; 2; a++)
  for (byte b = 0; b &lt; 3; b++)
  for (byte c = 0; c &lt; 4; c++)
  for (byte d = 0; d &lt; 3; d++)
  for (byte e = 0; e &lt; 4; e++)
  for (byte f = 0; f &lt; 3; f++)
  for (byte g = 0; g &lt; 3; g++)
  for (byte h = 0; h &lt; 4; h++)
  for (byte i = 0; i &lt; 2; i++)
  for (byte j = 0; j &lt; 4; j++)
  for (byte k = 0; k &lt; 4; k++)
  for (byte l = 0; l &lt; 3; l++)
  for (byte m = 0; m &lt; 4; m++)
  {
      data.Add(new [] {a, b, c, d, e, f, g, h, i, j, k, l, m});
  }
  </code></pre>
  
  <p>I was considering using something like a <code>BitArray</code> but I'm not sure how I could incorporate it.</p>
  
  <p>Any recommendations would be greatly appreciated. Alternatively, perhaps this is the fastest way of doing what I want?</p>
  
  <p><strong>EDIT</strong>
  Couple of quick points (and apologies I didn't put these in the original post):</p>
  
  <ul>
  <li>The numbers and the order of them (2, 3, 4, 3, 4, 3, 3 etc) are very important, so using a solution such as <a href="https://stackoverflow.com/questions/4319049/generating-permutations-using-linq">Generating Permutations using LINQ</a> won't help because the maximums in each 'column' are different</li>
  <li>I'm not a mathematician, so I apologise if I'm not using the technical terms like 'permutations' and 'combinations' correctly :)</li>
  <li>I <strong>do</strong> need to populate all of these combinations at once - I can't just grab one or another based on an index</li>
  <li>Using <code>byte</code> is faster than using <code>int</code>, I <strong>guarantee</strong> it. It's also a lot better on memory usage to have 67m+ arrays of bytes rather than ints</li>
  <li>My ultimate goal here is to look for a faster alternative to nested loops.</li>
  <li>I considered using parallel programming, but due to the iterative nature of what I'm trying to achieve, I couldn't find a way to do it successfully (even with <code>ConcurrentBag</code>) - however I'm happy to be proved wrong :)</li>
  </ul>
  
  <p><strong>CONCLUSION</strong></p>
  
  <p>Caramiriel has provided a good micro-optimisation which shaves some time off the loops, so I've marked that answer as correct. Eric also mentioned that it is faster to pre-allocate the List. But, at this stage it seems that the nested loops are in fact the fastest possible way of doing this (depressing, I know!).</p>
  
  <p>If you want to try exactly what I was trying to benchmark with <code>StopWatch</code>, go with 13 loops counting up to 4 in each loop - that makes about 67m+ lines in the list. On my machine (i5-3320M 2.6GHz) it takes about 2.2s to do the optimised version.</p>