Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Fastest solution is Buffer.BlockCopy:</p> <pre><code>static void ClearArray(Array array, int val) { var helper = Enumerable.Repeat(val, Math.Min(array.Length, 1024)).ToArray(); var itemSize = 4; Buffer.BlockCopy(helper, 0, array, 0, helper.Length * itemSize); for (var len = helper.Length; len &lt; array.Length; len *= 2) { Buffer.BlockCopy(array, 0, array, len * itemSize, Math.Min(len, array.Length - len) * itemSize); } } static int Count(Array array, Func&lt;int, bool&gt; predicate) { var helper = new int[Math.Min(array.Length, 4096)]; var itemSize = 4; var count = 0; for (var offset = 0; offset &lt; array.Length; offset += helper.Length) { var len = Math.Min(helper.Length, array.Length - offset); Buffer.BlockCopy(array, offset * itemSize, helper, 0, len * itemSize); for (var i = 0; i &lt; len; ++i) if (predicate(helper[i])) count++; } return count; } </code></pre> <p>Statistic:</p> <pre><code>time: 00:00:00.0449501, method: Buffer.BlockCopy time: 00:00:01.4371424, method: Lexicographical order time: 00:00:01.3588629, method: Recursed time: 00:00:06.2005057, method: Cartesian product with index array reusing time: 00:00:08.2433531, method: Cartesian product w/o index array reusing </code></pre> <p>Statistic (Count function):</p> <pre><code>time: 00:00:00.0812866, method: Buffer.BlockCopy time: 00:00:02.7617093, method: Lexicographical order </code></pre> <p>Code:</p> <pre><code> Array array = Array.CreateInstance(typeof(int), new[] { 100, 200, 400 }, new[] { -10, -20, 167 }); foreach (var info in new [] { new {Name = "Buffer.BlockCopy", Method = (Action&lt;Array, int&gt;)ClearArray_BufferCopy}, new {Name = "Lexicographical order", Method = (Action&lt;Array, int&gt;)ClearArray_LexicographicalOrder}, new {Name = "Recursed", Method = (Action&lt;Array, int&gt;)ClearArray_Recursed}, new {Name = "Cartesian product with index array reusing", Method = (Action&lt;Array, int&gt;)ClearArray_Cartesian_ReuseArray}, new {Name = "Cartesian product w/o index array reusing", Method = (Action&lt;Array, int&gt;)ClearArray_Cartesian}, } ) { var stopwatch = new Stopwatch(); stopwatch.Start(); var count = 10; for (var i = 0; i &lt; count; ++i) info.Method(array, i); stopwatch.Stop(); Console.WriteLine("time: {0}, method: {1}", TimeSpan.FromTicks(stopwatch.Elapsed.Ticks / count), info.Name); } </code></pre>
    singulars
    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. 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