Efficient Array Retrieval

Is there an efficient way to take a subset of a C # array and pass it to another piece of code (without changing the original array)? I am using CUDA.net, which has a function that copies an array to the GPU. For example, I would like to pass the function of the 10th array and thus copy every tenth array to the GPU separately (for pipelining purposes).

Copying an array in this way should be as efficient as copying it at a time. This can be done using unsafe code and simply refer to the desired memory location, but not that I'm not sure. The CopyTo function copies the entire array to another array, so this does not seem to be useful.

+5
source share
4 answers

Well, I misunderstood the question before.

Do you want System.Buffer.BlockCopy or System.Array.Copy .

LINQ methods will be terribly inefficient. If you can reuse the buffer you are copying to, this will also help increase efficiency by avoiding creating a new array every time - just copy from above. If you cannot split your "large" array the same way, you will need a new one for the latter case.

+11
source

I'm not sure how effective this is, but ...

int[] myInts = new int[100];

//Code to populate original arrray

for (int i = 0; i < myInts.Length; i += 10)
{
    int[] newarray = myInts.Skip(i).Take(10).ToArray();
    //Do stuff with new array
}
+3
source

Marshal.Copy, . .

: , API. - . .

, , , api

void CopyToGpu(byte[] source, int start, int length);
+1

You can use the extension methods and return the income:

public static IEnumerable Part<T>(this T[] array, int startIndex, int endIndex )
{
    for ( var currentIndex = startIndex; currentIndex < endIndex; ++currentIndex )
        yield return array[currentIndex];
}
0
source

All Articles