我有3个渠道:
byte[] Red; byte[] Green; byte[] Blue;
我需要从他们所有的值复制到字节[Red.Length + Green.Length + Blue.Length] PA ,这样:
PA[0] = Red[0]; PA[1] = Green[0]; PA[2] = Blue[0]; PA[3] = Red[1]; /// and so on下面是与上述数组的一个例子:
Here is an example with the above arrays:
byte[] Red = new byte[255]; byte[] Green = new byte[255]; byte[] Blue = new byte[255]; byte[] PA = new byte[Red.Length + Green.Length + Blue.Length]; for (int i = 0; i != 255; ++i) { PA[i*3 + 0] = Red[i]; PA[i*3 + 1] = Green[i]; PA[i*3 + 2] = Blue[i]; }
我假设要合并的集合是大小相等的,他们确实有一些为了彼此之间如 [0] =红色, [1] =绿色等,有以preserved的在合并藏品。
I'm assuming that the collections to be merged are of equal sizes and that they do have some order amongst themselves e.g. [0] = Red, [1]=Green, etc. that has to be preserved for the items in the "merged" collection.
什么是最有效的方式做到这一点 C#?集合不必是数组或物品字节(尽管接受字节集合类型。将AP preciated)。
What is the most efficient way to do this in C#? The collections do not have to be arrays nor the items bytes (although collection types that accept bytes would be appreciated).
推荐答案我试图用三分球做出一个比较有效的方式:
I tried to make a more efficent way by using pointers:
unsafe { fixed (byte* red = Red, green = Green, blue = Blue, pa = PA2) { byte* r = red, g = green, b = blue, p = pa; for (int i = 0; i < 255; i++) { *p = *r; p++; r++; *p = *g; p++; g++; *p = *b; p++; b++; } } }在x86的模式下,这是大约快一倍,但在64位模式没有什么区别。
In x86 mode this is about twice as fast, but in x64 mode there is no difference.
在最后,code,你必须是已经足够快于大多数应用。如果你需要它是非常快,您可以优化有点,但数量不多。
In conclusion, the code that you have is already fast enough for most applications. If you need it to be really fast you can optimise it a bit, but not much.
更多推荐
合并集合preserving为了最有效的方法是什么?
发布评论