将 2 个连续字节转换为一个 int 值提高 C# 中的速度
Convert 2 successive Bytes to one int value Increase speed in C#
我需要将两个 Bytes 组合成一个 int 值。
我从我的相机收到一张 16 位图像,两个连续的字节具有一个像素的强度值。我的目标是将这两个字节组合成一个 "int" 值。
我设法使用以下代码完成此操作:
for (int i = 0; i < VectorLength * 2; i = i + 2)
{
NewImageVector[ImagePointer] = ((int)(buffer.Array[i + 1]) << 8) | ((int)(buffer.Array[i]));
ImagePointer++;
}
我的图像是 1280*960,所以 VectorLength==1228800,传入缓冲区大小是 2*1228800=2457600 个元素...
有什么办法可以加快速度吗?
也许还有另一种方法,所以我不需要使用 for 循环。
谢谢
您可以使用与 c 的并集等效的方法。我不确定是否更快,但更优雅:
[StructLayout(LayoutKind.Explicit)]
struct byte_array
{
[FieldOffset(0)]
public byte byte1;
[FieldOffset(1)]
public byte byte2;
[FieldOffset(0)]
public short int0;
}
像这样使用它:
byte_array ba = new byte_array();
//insert the two bytes
ba.byte1 = (byte)(buffer.Array[i]);
ba.byte2 = (byte)(buffer.Array[i + 1]);
//get the integer
NewImageVector[ImagePointer] = ba.int1;
你可以填充你的两个字节并使用int。要找到更快的方法,请使用 StopWatch-Class 并像这样比较两种方法:
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
//The code
stopWatch.Stop();
MessageBox.Show(stopWatch.ElapsedTicks.ToString()); //Or milliseconds ,...
您可以通过使用不安全指针迭代数组来实现小幅性能提升。以下代码假定 source
是输入字节数组(在您的情况下为 buffer.Array
)。它还假定 source
具有偶数个元素。在生产代码中,您显然必须检查这些内容。
int[] output = new int[source.Length / 2];
fixed (byte* pSource = source)
fixed (int* pDestination = output)
{
byte* sourceIterator = pSource;
int* destIterator = pDestination;
for (int i = 0; i < output.Length; i++)
{
(*destIterator) = ((*sourceIterator) | (*(sourceIterator + 1) << 8));
destIterator++;
sourceIterator += 2;
}
}
return output;
假设您可以(重新)将 NewImageVector
定义为 short[]
,并且 Buffer
中的每两个连续字节应转换为 short
(基本上你现在在做什么,只有你之后投射到 int
),你可以使用 Buffer.BlockCopy 为你做。
如文档所述,您 Buffer.BlockCopy
将字节从一个数组复制到另一个数组,因此为了将字节复制到缓冲区中,您需要执行以下操作:
Buffer.BlockCopy(Buffer, 0, NewImageVector, 0, [NumberOfExpectedShorts] * 2)
这告诉 BlockCopy
你想从 Buffer
开始复制字节,从索引 0 开始,到 NewImageVector
从索引 0 开始,你想复制 [NumberOfExpectedShorts] * 2
字节(因为每个 short 都是两个字节长)。
没有循环,但它确实取决于使用 short[]
数组而不是 int[]
数组的能力(实际上,取决于使用数组开始)。
请注意,这还要求 Buffer
中的字节按 little-endian
顺序排列(即 Buffer[index]
包含低字节,buffer[index + 1]
包含高字节)。
我需要将两个 Bytes 组合成一个 int 值。
我从我的相机收到一张 16 位图像,两个连续的字节具有一个像素的强度值。我的目标是将这两个字节组合成一个 "int" 值。
我设法使用以下代码完成此操作:
for (int i = 0; i < VectorLength * 2; i = i + 2)
{
NewImageVector[ImagePointer] = ((int)(buffer.Array[i + 1]) << 8) | ((int)(buffer.Array[i]));
ImagePointer++;
}
我的图像是 1280*960,所以 VectorLength==1228800,传入缓冲区大小是 2*1228800=2457600 个元素...
有什么办法可以加快速度吗? 也许还有另一种方法,所以我不需要使用 for 循环。
谢谢
您可以使用与 c 的并集等效的方法。我不确定是否更快,但更优雅:
[StructLayout(LayoutKind.Explicit)]
struct byte_array
{
[FieldOffset(0)]
public byte byte1;
[FieldOffset(1)]
public byte byte2;
[FieldOffset(0)]
public short int0;
}
像这样使用它:
byte_array ba = new byte_array();
//insert the two bytes
ba.byte1 = (byte)(buffer.Array[i]);
ba.byte2 = (byte)(buffer.Array[i + 1]);
//get the integer
NewImageVector[ImagePointer] = ba.int1;
你可以填充你的两个字节并使用int。要找到更快的方法,请使用 StopWatch-Class 并像这样比较两种方法:
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
//The code
stopWatch.Stop();
MessageBox.Show(stopWatch.ElapsedTicks.ToString()); //Or milliseconds ,...
您可以通过使用不安全指针迭代数组来实现小幅性能提升。以下代码假定 source
是输入字节数组(在您的情况下为 buffer.Array
)。它还假定 source
具有偶数个元素。在生产代码中,您显然必须检查这些内容。
int[] output = new int[source.Length / 2];
fixed (byte* pSource = source)
fixed (int* pDestination = output)
{
byte* sourceIterator = pSource;
int* destIterator = pDestination;
for (int i = 0; i < output.Length; i++)
{
(*destIterator) = ((*sourceIterator) | (*(sourceIterator + 1) << 8));
destIterator++;
sourceIterator += 2;
}
}
return output;
假设您可以(重新)将 NewImageVector
定义为 short[]
,并且 Buffer
中的每两个连续字节应转换为 short
(基本上你现在在做什么,只有你之后投射到 int
),你可以使用 Buffer.BlockCopy 为你做。
如文档所述,您 Buffer.BlockCopy
将字节从一个数组复制到另一个数组,因此为了将字节复制到缓冲区中,您需要执行以下操作:
Buffer.BlockCopy(Buffer, 0, NewImageVector, 0, [NumberOfExpectedShorts] * 2)
这告诉 BlockCopy
你想从 Buffer
开始复制字节,从索引 0 开始,到 NewImageVector
从索引 0 开始,你想复制 [NumberOfExpectedShorts] * 2
字节(因为每个 short 都是两个字节长)。
没有循环,但它确实取决于使用 short[]
数组而不是 int[]
数组的能力(实际上,取决于使用数组开始)。
请注意,这还要求 Buffer
中的字节按 little-endian
顺序排列(即 Buffer[index]
包含低字节,buffer[index + 1]
包含高字节)。