C# 高效获取数组子集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/268513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:51:09  来源:igfitidea点击:

Getting array subsets efficiently

c#arrayscudacuda.net

提问by Morten Christiansen

Is there an efficient way to take a subset of a C# array and pass it to another peice of code (without modifying the original array)? I use CUDA.net which has a function which copies an array to the GPU. I would like to e.g. pass the function a 10th of the array and thus copy each 10th of the array to the GPU seperately (for pipelining purposes).

有没有一种有效的方法来获取 C# 数组的子集并将其传递给另一段代码(而不修改原始数组)?我使用 CUDA.net,它具有将数组复制到 GPU 的功能。例如,我想将函数传递到数组的第 10 个,从而将数组的每个第 10 个单独复制到 GPU(用于流水线目的)。

Copying the array in this way should be as efficient as copying it in one go. It can be done with unsafe code and just referencing the proper memory location but other than that I'm not sure. The CopyTo function copies the entire array to another array so this does not appear useful.

以这种方式复制数组应该和一次性复制一样有效。它可以用不安全的代码完成,并且只引用正确的内存位置,但除此之外我不确定。CopyTo 函数将整个数组复制到另一个数组,因此这似乎没有用。

采纳答案by Jon Skeet

Okay, I'd misunderstood the question before.

好吧,我之前误解了这个问题。

What you want is System.Buffer.BlockCopyor System.Array.Copy.

你想要的是System.Buffer.BlockCopySystem.Array.Copy

The LINQ ways will be hideously inefficient. If you're able to reuse the buffer you're copying into, that will also help the efficiency, avoiding creating a new array each time - just copy over the top. Unless you can divide your "big" array up equally though, you'll need a new one for the last case.

LINQ 的方式将非常低效。如果您能够重用要复制到的缓冲区,这也将有助于提高效率,避免每次都创建一个新数组 - 只需复制顶部即可。除非您可以平均划分“大”数组,否则最后一种情况需要一个新数组。

回答by Eoin Campbell

I'm not sure how efficient 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
}

回答by k?e?m?p? ?

You could use extension methods and yield return:

您可以使用扩展方法和收益回报:

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

回答by plinth

You could try Marshal.Copy if you need to go from an array of bytes to an unmanaged pointer. That avoids creating unsafe code yourself.

如果您需要从字节数组转到非托管指针,您可以尝试 Marshal.Copy。这样可以避免自己创建不安全的代码。

Edit: This would clearly only work if you reimplement their API. Sorry - misunderstood. You want an efficient subarray method.

编辑:这显然只有在您重新实现他们的 API 时才有效。对不起 - 误解了。您需要一种高效的子数组方法。

It strikes me that what you really want is an api in the original class of the form

我觉得你真正想要的是表单原始类中的 api

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