简体   繁体   English

创建NumberSequence数组的最快方法

[英]Fastest Way To Create an Array of a NumberSequence

I would like to create an array with of length X, and i would like the following 'inteligence' 我想创建一个长度为X的数组,我想下面的“智能”

if , for exemple, X = 6, myArray[x] = [0,1,2,3,4,5] 如果,例如X = 6,则myArray [x] = [0,1,2,3,4,5]

For the moment, i do 目前,我确实

int[] availableIndex = new int[DestructiblesCubes.Count];
for (var i = 0; i < availableIndex.Length; i++)
{
    availableIndex[i] = i;
}

But, i'm curious, is there a better (the faster way to execute it) and/or the faster(the shortest char length) way? 但是,我很好奇,是否有更好的(执行它的更快方法)和/或更快的(最短字符长度)方法?

Thanks :) 谢谢 :)

This is short way to implement this. 这是实现此目的的捷径。 Not the performance best solution. 不是性能最佳的解决方案。

Enumerable.Range(0, 10).ToArray()

MSDN description for Enumerable.Range 有关Enumerable.Range的 MSDN描述

I think the fastest method uses the unsafe context together with a proper fixed pointer to the array, as demonstrated below: 我认为最快的方法使用unsafe上下文以及指向数组的正确fixed指针,如下所示:

/*const*/ int availableIndex_Length = 6;
int[] availableIndex = new int[availableIndex_Length];
unsafe {
    fixed(int* p = &availableIndex[0]) {
        for(int i = 0; i < availableIndex_Length; ++i) {
            *(p+i) = i;
        }
    } 
}

This can be refactored to a method, optionally inline d: 可以将其重构为方法,可以选择inline d:

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static unsafe void FillRange(ref int[] array) {
      int length = array.Length;
      fixed(int* p = &array[0]) {
          for(int i = 0; i < length; ++i) {
              *(p + i) = i;
          }
      }
}

static void Main(string[] args) {
    // Example usage:
    int[] availableIndices = new int[6];
    FillRange(ref availableIndices);

    // Test if it worked:
    foreach(var availableIndex in availableIndices) {
        Console.WriteLine(availableIndex);
    }
    Console.ReadKey(true);
}

The only optimisation I can see to apply to your code as it stands is to count down to zero, but any increase in performance will be tiny 我可以看到的唯一适用于您的代码的优化是递减计数到零,但是性能的提高将很小

int[] availableIndex = new int[DestructiblesCubes.Count];
for (var i = availableIndex.Length-1; i >= 0; i--)
{
    availableIndex[i] = i;
}

Otherwise, especially if you're talking large arrays, one thing to try would be to create an array greater than your max envisioned value of DestructiblesCubes.Count and Intialize that array as above, then use Array.Copy when you want the smaller array. 否则,尤其是在谈论大型数组时,要尝试做的一件事是创建一个大于DestructiblesCubes.Count的最大预想值的数组,并如上所述对这个数组进行初始化,然后在需要较小的数组时使用Array.Copy
I would be confident that no code we hand roll will be faster than a single call to Array.Copy . 我相信,我们手动编写的代码不会比对Array.Copy的单次调用Array.Copy

int[] availableIndex = new int[DestructiblesCubes.Count];
Array.Copy(LargeArray, availableIndex, availableIndex.Length);

Otherwise I can't think of anything that might be faster than the code you have. 否则,我想不出任何比您的代码更快的方法。

You could try this: 您可以尝试以下方法:

unsafe
{
    int[] availableIndex = new int[DestructiblesCubes.Count];
    int length = availableIndex.Length;
    int n = 0;
    fixed(int *p = availableIndex) {
        while(n < length) *p++ = n++;
    }
}

may be faster, depending on the optimization stage of your compiler. 可能更快,具体取决于编译器的优化阶段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM