Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For primitive types (including bytes), use <code>System.Buffer.BlockCopy</code> instead of <code>System.Array.Copy</code>. It's faster.</p> <p>I timed each of the suggested methods in a loop executed 1 million times using 3 arrays of 10 bytes each. Here are the results:</p> <ol> <li>New Byte Array using <code>System.Array.Copy</code> - 0.2187556 seconds</li> <li>New Byte Array using <code>System.Buffer.BlockCopy</code> - 0.1406286 seconds</li> <li>IEnumerable&lt;byte> using C# yield operator - 0.0781270 seconds</li> <li>IEnumerable&lt;byte> using LINQ's Concat&lt;> - 0.0781270 seconds</li> </ol> <p>I increased the size of each array to 100 elements and re-ran the test:</p> <ol> <li>New Byte Array using <code>System.Array.Copy</code> - 0.2812554 seconds</li> <li>New Byte Array using <code>System.Buffer.BlockCopy</code> - 0.2500048 seconds</li> <li>IEnumerable&lt;byte> using C# yield operator - 0.0625012 seconds</li> <li>IEnumerable&lt;byte> using LINQ's Concat&lt;> - 0.0781265 seconds</li> </ol> <p>I increased the size of each array to 1000 elements and re-ran the test:</p> <ol> <li>New Byte Array using <code>System.Array.Copy</code> - 1.0781457 seconds</li> <li>New Byte Array using <code>System.Buffer.BlockCopy</code> - 1.0156445 seconds</li> <li>IEnumerable&lt;byte> using C# yield operator - 0.0625012 seconds</li> <li>IEnumerable&lt;byte> using LINQ's Concat&lt;> - 0.0781265 seconds</li> </ol> <p>Finally, I increased the size of each array to 1 million elements and re-ran the test, executing each loop <strong>only</strong> 4000 times:</p> <ol> <li>New Byte Array using <code>System.Array.Copy</code> - 13.4533833 seconds</li> <li>New Byte Array using <code>System.Buffer.BlockCopy</code> - 13.1096267 seconds</li> <li>IEnumerable&lt;byte> using C# yield operator - 0 seconds</li> <li>IEnumerable&lt;byte> using LINQ's Concat&lt;> - 0 seconds</li> </ol> <p>So, if you need a new byte array, use</p> <pre><code>byte[] rv = new byte[a1.Length + a2.Length + a3.Length]; System.Buffer.BlockCopy(a1, 0, rv, 0, a1.Length); System.Buffer.BlockCopy(a2, 0, rv, a1.Length, a2.Length); System.Buffer.BlockCopy(a3, 0, rv, a1.Length + a2.Length, a3.Length); </code></pre> <p>But, if you can use an <code>IEnumerable&lt;byte&gt;</code>, <strong><em>DEFINITELY</em></strong> prefer LINQ's Concat&lt;> method. It's only slightly slower than the C# yield operator, but is more concise and more elegant.</p> <pre><code>IEnumerable&lt;byte&gt; rv = a1.Concat(a2).Concat(a3); </code></pre> <p>If you have an arbitrary number of arrays and are using .NET 3.5, you can make the <code>System.Buffer.BlockCopy</code> solution more generic like this:</p> <pre><code>private byte[] Combine(params byte[][] arrays) { byte[] rv = new byte[arrays.Sum(a =&gt; a.Length)]; int offset = 0; foreach (byte[] array in arrays) { System.Buffer.BlockCopy(array, 0, rv, offset, array.Length); offset += array.Length; } return rv; } </code></pre> <p>*Note: The above block requires you adding the following namespace at the the top for it to work.</p> <pre><code>using System.Linq; </code></pre> <p>To Jon Skeet's point regarding iteration of the subsequent data structures (byte array vs. IEnumerable&lt;byte>), I re-ran the last timing test (1 million elements, 4000 iterations), adding a loop that iterates over the full array with each pass:</p> <ol> <li>New Byte Array using <code>System.Array.Copy</code> - 78.20550510 seconds</li> <li>New Byte Array using <code>System.Buffer.BlockCopy</code> - 77.89261900 seconds</li> <li>IEnumerable&lt;byte> using C# yield operator - 551.7150161 seconds</li> <li>IEnumerable&lt;byte> using LINQ's Concat&lt;> - 448.1804799 seconds</li> </ol> <p>The point is, it is <strong><em>VERY</em></strong> important to understand the efficiency of both the creation <em>and the usage</em> of the resulting data structure. Simply focusing on the efficiency of the creation may overlook the inefficiency associated with the usage. Kudos, Jon.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload