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

C# 用单个值填充数组的最快方法,c#,algorithm,performance,optimization,memory,C#,Algorithm,Performance,Optimization,Memory,我想用我拥有的一个值填充一个2D数组,但是,我想用最快的方法填充2D数组,2D数组的总长度将是200k+,随着时间的推移,这些数组将超过200个。我已经研究了Buffer.BlockCopy和Array.Copy,但是,它们都将数组作为源/目标,其中我唯一拥有的数组是目标,而源是单个值 使用源为单个值而不是数组填充数组的最快方法是什么?有关一些相关信息,请参阅 正如在那个问题中提到的(非常接近于这个问题的重复),for循环通常是最好的,除非您想进入 所以这应该很快: int[] arr = ne

我想用我拥有的一个值填充一个2D数组,但是,我想用最快的方法填充2D数组,2D数组的总长度将是200k+,随着时间的推移,这些数组将超过200个。我已经研究了Buffer.BlockCopy和Array.Copy,但是,它们都将数组作为源/目标,其中我唯一拥有的数组是目标,而源是单个值


使用源为单个值而不是数组填充数组的最快方法是什么?

有关一些相关信息,请参阅

正如在那个问题中提到的(非常接近于这个问题的重复),for循环通常是最好的,除非您想进入

所以这应该很快:

int[] arr = new int[MAX_ELEMENTS];
for (int i = 0; i < arr.Length; ++i)
{
    array[i] = MY_VALUE;
}
int[]arr=新的int[MAX_元素];
对于(int i=0;i

与所有与性能相关的事情一样,让某些东西工作起来,然后测量瓶颈是什么。强调“度量”。试图猜测瓶颈是什么通常是个坏主意(:

数组。复制可能比for循环优化得更好,所以使用它

void FillArray<T>(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;
    }
}
void FillArray(T[]arr,T fillValue)
{
int i=0;
如果(arr.Length>16){
{
做{
数组[i++]=fillValue;
}而(i

(对于不同的类型和数组大小,我希望看到它与naive for循环之间的性能比较)

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

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

using System;

namespace Extensions
{
    public static class ArrayExtensions
    {
        public static void Fill<T>(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);
        }
    }
}
使用系统;
命名空间扩展
{
公共静态类ArrayExtensions
{
公共静态空白填充(此T[]目标数组,参数T[]值)
{
if(destinationArray==null)
{
抛出新ArgumentNullException(“destinationArray”);
}
如果(value.Length>=destinationArray.Length)
{
抛出新ArgumentException(“值数组的长度必须小于目标的长度”);
}
//设置初始数组值
Copy(value,destinationArray,value.Length);
int-ArrayFillHalfLength=destinationArray.Length/2;
int字长;

对于(copyLength=value.Length;copyLengthFill
扩展名,并将签名更改为
publicstaticvoidfill(这个T[,]destinationArray,T[,]value)
并这样命名:
myLargeArray.Fill(new[,]{{double.NaN},{double.NaN});
它工作得很好。谢谢!