使用单个值填充数组的最快方法

我想用我所拥有的单个值来填充2D数组,但是,我想尽可能以最快的方式完成2D数组的长度总计200k +并且随着时间的推移将有超过200个这样的数组。 我已经查看了Buffer.BlockCopy和Array.Copy,但是,它们都将数组作为源/目标,其中我唯一的数组是目标,源是单个值。

填充数组的最快方法是源是单个值而不是数组?

我发现的最快的方法是使用Array.Copy,每次循环时副本大小加倍。 无论是使用单个值还是数组值填充数组,速度基本相同。

在我的20,000,000个数组项的测试中,这个函数的速度是for循环的两倍。

using System; namespace Extensions { public static class ArrayExtensions { public static void Fill(this T[] destinationArray, params T[] value) { if (destinationArray == null) { throw new ArgumentNullException("destinationArray"); } if (value.Length >= destinationArray.Length) { throw new ArgumentException("Length of value array must be less than length of destination"); } // set the initial array value Array.Copy(value, destinationArray, value.Length); int arrayToFillHalfLength = destinationArray.Length / 2; int copyLength; for(copyLength = value.Length; copyLength < arrayToFillHalfLength; copyLength <<= 1) { Array.Copy(destinationArray, 0, destinationArray, copyLength, copyLength); } Array.Copy(destinationArray, 0, destinationArray, copyLength, destinationArray.Length - copyLength); } } } 

我在http://coding.grax.com/2011/11/initialize-array-to-value-in-c-very.html和http://coding.grax.com/2014/04/better上写了这篇博文。 -arrays-填充- function.html

有关相关信息,请参阅C#中memset的等效内容是什么? 。

正如在那个问题中提到的那样(非常接近这个问题),除非你想进入非托管代码,否则for循环通常是最好的。

所以这应该很快:

 int[] arr = new int[MAX_ELEMENTS]; for (int i = 0; i < arr.Length; ++i) { array[i] = MY_VALUE; } 

与性能相关的所有事情一样,让事情有效,然后衡量瓶颈是什么。 强调“措施”。 试图猜测瓶颈是什么通常是一个坏主意(:

Array.Copy可能比for循环更好地优化,所以使用它。

 void FillArray(T[] arr, T fillValue) { int i = 0; if (arr.Length > 16) { { do { array[i++] = fillValue; } while (i < arr.Length) while (i + 16 < arr.Length) { Array.Copy(arr, 0, arr, i, 16); i = i + 16; } } while (i < arr.Length) { array[i++] = fillValue; } } 

(我很想看到这个和天真的for循环之间的性能比较,对于不同的类型和数组大小)