Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two obvious approaches: use <code>Array.Copy</code> and copy elements one by one:</p> <pre><code>private static void ElementByElement&lt;T&gt;(T[] arg, int start) { for (int i = arg.Length - 1; i &gt; start; i--) { arg[i] = arg[i - 1]; } } private static T BuiltInCopy&lt;T&gt;(T[] arg, int start) { Array.Copy(arg, start, arg, start + 1, arg.Length - start - 1); return arg[0]; } </code></pre> <p>On one hand, <code>List&lt;T&gt;</code> insert uses <code>Array.Copy</code>. On the other, <code>Array.Copy</code> is non-generic and can do a <em>lot</em> of extra work: <a href="http://msdn.microsoft.com/en-us/library/z50k9bft.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/z50k9bft.aspx</a></p> <p>I expected <code>Array.Copy</code> to be faster. However, the results of this MiniBench test surprised me.</p> <pre><code>internal class Program { private static int smallArraySize = 32; public static void Main(string[] args) { BenchArrayCopy(); } private static void BenchArrayCopy() { var smallArrayInt = new int[smallArraySize]; for (int i = 0; i &lt; smallArraySize; i++) smallArrayInt[i] = i; var smallArrayString = new string[smallArraySize]; for (int i = 0; i &lt; smallArraySize; i++) smallArrayString[i] = i.ToString(); var smallArrayDateTime = new DateTime[smallArraySize]; for (int i = 0; i &lt; smallArraySize; i++) smallArrayDateTime[i] = DateTime.Now; var moveInt = new TestSuite&lt;int[], int&gt;("Move part of array right by 1: int") .Plus(BuiltInCopy, "Array.Copy()") .Plus(ElementByElement, "Element by element in a for loop") .RunTests(smallArrayInt, 0); var moveString = new TestSuite&lt;string[], string&gt;("Move part of array right by 1: string") .Plus(BuiltInCopy, "Array.Copy()") .Plus(ElementByElement, "Element by element in a for loop") .RunTests(smallArrayString, "0"); var moveDT = new TestSuite&lt;DateTime[], DateTime&gt;("Move part of array right by 1: DateTime") .Plus(BuiltInCopy, "Array.Copy()") .Plus(ElementByElement, "Element by element in a for loop") .RunTests(smallArrayDateTime, smallArrayDateTime[0]); moveInt.Display(ResultColumns.All, moveInt.FindBest()); moveString.Display(ResultColumns.All, moveInt.FindBest()); moveDT.Display(ResultColumns.All, moveInt.FindBest()); } private static T ElementByElement&lt;T&gt;(T[] arg) { int start = 8; for (int i = smallArraySize - 1; i &gt; start; i--) { arg[i] = arg[i - 1]; } return arg[0]; } private static T BuiltInCopy&lt;T&gt;(T[] arg) { int start = 8; int length = smallArraySize - start - 1; Array.Copy(arg, start, arg, start + 1, length); return arg[0]; } } </code></pre> <p>Here are the results from two runs:</p> <pre><code>f:\MyProgramming\TimSort\Benchmarks\bin\Release&gt;Benchmarks.exe ============ Move part of array right by 1: int ============ Array.Copy() 568475865 0:31.606 1,73 Element by element in a for loop 980013061 0:31.449 1,00 ============ Move part of array right by 1: string ============ Array.Copy() 478224336 0:31.618 2,06 Element by element in a for loop 220168237 0:30.926 4,38 ============ Move part of array right by 1: DateTime ============ Array.Copy() 382906030 0:27.870 2,27 Element by element in a for loop 458265102 0:29.239 1,99 f:\MyProgramming\TimSort\Benchmarks\bin\Release&gt;Benchmarks.exe ============ Move part of array right by 1: int ============ Array.Copy() 500925013 0:28.514 1,76 Element by element in a for loop 988394220 0:31.967 1,00 ============ Move part of array right by 1: string ============ Array.Copy() 483178262 0:30.048 1,92 Element by element in a for loop 193092931 0:27.642 4,43 ============ Move part of array right by 1: DateTime ============ Array.Copy() 450569361 0:30.807 2,11 Element by element in a for loop 568054290 0:31.385 1,71 </code></pre> <p>That is, for <code>int</code> and <code>DateTime</code> <code>ElementByElement</code> is significantly faster; while for <code>string</code> <code>BuiltInCopy</code> is over twice as fast as <code>ElementByElement</code> (and twice as slow as <code>ElementByElement</code> for <code>int</code>). I'd expect the results for <code>int</code> and <code>string</code> to be very similar on a 32-bit machine, since a reference to <code>string</code> on the stack is the same size as an <code>int</code> and no operations other than reading and writing stack memory should be involved.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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