c# - Fastest Way To Create an Array of a NumberSequence -
i create array of length x, , following 'inteligence'
if , exemple, x = 6, myarray[x] = [0,1,2,3,4,5]
for moment,
int[] availableindex = new int[destructiblescubes.count]; (var = 0; < availableindex.length; i++) { availableindex[i] = i; }
but, i'm curious, there better (the faster way execute it) and/or faster(the shortest char length) way?
thanks :)
i think fastest method uses unsafe
context proper fixed
pointer array, demonstrated below:
/*const*/ int availableindex_length = 6; int[] availableindex = new int[availableindex_length]; unsafe { fixed(int* p = &availableindex[0]) { for(int = 0; < availableindex_length; ++i) { *(p+i) = i; } } }
this can refactored method, optionally inline
d:
[methodimpl(methodimploptions.aggressiveinlining)] static unsafe void fillrange(ref int[] array) { int length = array.length; fixed(int* p = &array[0]) { for(int = 0; < length; ++i) { *(p + i) = i; } } } static void main(string[] args) { // example usage: int[] availableindices = new int[6]; fillrange(ref availableindices); // test if worked: foreach(var availableindex in availableindices) { console.writeline(availableindex); } console.readkey(true); }
Comments
Post a Comment