合并集合preserving为了最有效的方法是什么?

编程入门 行业动态 更新时间:2024-10-28 00:22:37
本文介绍了合并集合preserving为了最有效的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有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为了最有效的方法是什么?

本文发布于:2023-10-25 00:22:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1525408.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最有效   方法   preserving

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!