将数组从x复制到y而不是x复制到array.length

时间:2021-02-22 21:39:05

I've tried copying arrays in such a way I can crunch data in an array with threads but obviously without splitting the array into smaller chunks (lets say 1 array -> 4 quarters (4 arrays)).

我试过以这样的方式复制数组我可以用线程处理数组中的数据,但显然没有将数组拆分成更小的块(假设1个数组 - > 4个季度(4个数组))。

The only method I can find copies from a specified (int)start point and copies all leading data from the start to the end which if I am using multiple threads to crunch the data its nullifies the point of threading.

我可以从指定的(int)起始点找到副本的唯一方法,并将所有前导数据从开始复制到结尾,如果我使用多个线程来处理数据,则会使线程无效。

Here is pseudo code to show what I wish to do.

这是伪代码,以显示我想做什么。

int array { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }
int split1 { 0, 1, 2, 3 }
int split2 { 4, 5, 6, 7 }
int split3 { 8, 9, 10, 11 }
int split4 { 12, 13, 14, 15 }

or lets say the length of the array cant be split up evenly

或者说,阵列的长度不能均匀分开

int array { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }
int split1 { 0, 1, 2, 3 }
int split2 { 4, 5, 6, 7 }
int split3 { 8, 9, 10, 11 }
int split4 { 12, 13, 14, 15, 16}

1 个解决方案

#1


2  

The only method I can find copies from a specified (int)start point and copies all leading data from the start to the end which if I am using multiple threads to crunch the data its nullifies the point of threading.

我可以从指定的(int)起始点找到副本的唯一方法,并将所有前导数据从开始复制到结尾,如果我使用多个线程来处理数据,则会使线程无效。

It's a shame you didn't show which method that was. Array.Copy has various overloads for copying part of an array to another array. This one is probably the most helpful:

遗憾的是你没有显示出哪种方法。 Array.Copy具有各种重载,用于将数组的一部分复制到另一个数组。这个可能是最有帮助的:

public static void Copy(
    Array sourceArray,
    int sourceIndex,
    Array destinationArray,
    int destinationIndex,
    int length
)

Alternatively, look at Buffer.BlockCopy, which has basically the same signature - but the values are all in terms of bytes rather than array indexes. It also only works with arrays of primitives.

或者,查看Buffer.BlockCopy,它具有基本相同的签名 - 但值都是以字节而不是数组索引。它也只适用于基元数组。

Another alternative would be not to create copies of the array at all - if each thread knows which segment of the array it should work with, it can access that directly. You should also look into Parallel.ForEach (and similar methods) as a way of parallelizing operations easily at a higher level.

另一种选择是根本不创建数组的副本 - 如果每个线程知道它应该使用的数组的哪个部分,它可以直接访问它。您还应该将Parallel.ForEach(和类似方法)作为一种在更高级别轻松并行化操作的方法。

#1


2  

The only method I can find copies from a specified (int)start point and copies all leading data from the start to the end which if I am using multiple threads to crunch the data its nullifies the point of threading.

我可以从指定的(int)起始点找到副本的唯一方法,并将所有前导数据从开始复制到结尾,如果我使用多个线程来处理数据,则会使线程无效。

It's a shame you didn't show which method that was. Array.Copy has various overloads for copying part of an array to another array. This one is probably the most helpful:

遗憾的是你没有显示出哪种方法。 Array.Copy具有各种重载,用于将数组的一部分复制到另一个数组。这个可能是最有帮助的:

public static void Copy(
    Array sourceArray,
    int sourceIndex,
    Array destinationArray,
    int destinationIndex,
    int length
)

Alternatively, look at Buffer.BlockCopy, which has basically the same signature - but the values are all in terms of bytes rather than array indexes. It also only works with arrays of primitives.

或者,查看Buffer.BlockCopy,它具有基本相同的签名 - 但值都是以字节而不是数组索引。它也只适用于基元数组。

Another alternative would be not to create copies of the array at all - if each thread knows which segment of the array it should work with, it can access that directly. You should also look into Parallel.ForEach (and similar methods) as a way of parallelizing operations easily at a higher level.

另一种选择是根本不创建数组的副本 - 如果每个线程知道它应该使用的数组的哪个部分,它可以直接访问它。您还应该将Parallel.ForEach(和类似方法)作为一种在更高级别轻松并行化操作的方法。