我需要从一个提供长度为 332 个元素的数据数组(这些元素的数据类型为 short)的设备中连续填充 16384 个双精度元素的数组。 目前复制需要 28 毫秒来填充 16384 元素数组。 我想至少在 10 毫秒内得到这个。 在下面的代码中,方法getData返回两个包含332个元素(iBuf和qBuf)的短数组。 此方法需要 14 个即时报价 (3uS),因此与速度无关。
getData();
while (keepGoing)
{
for (int i = 0; i < 16384; i++)
{
iData[i] = ibuf[rawCounter];
qData[i] = qbuf[rawCounter];
rawCounter++;
if (rawCounter == samplesPerPacket)
{
getData();
rawCounter = 0;
}
//processing of data occurs here
}
感谢您的任何帮助和建议
使用 Array.copy 方法可能会有所帮助
while(keeping)
{
Array.Copy(ibuf,0,iData,counter,iData.Length)
counter += iData.Length
//Exit while once you hit 16384
//Might also need to check you don't overflow buffer since 16384 doesn't divide evenly into 332.
}
首先,您的代码不会按原样编译。尝试编辑以制作您想要完成的操作的最小示例。您缺少初始化(new
语句),看起来您正在用 C# 编写 Java 代码。
至少使用Array.Copy()
.或者,您可以使用指针(如果缓冲区包含内部值)或如前所述BlockCopy()
复制bytes
。使用 sizeof()
函数查找每个元素的字节数。
您可以使用以下技术,我们利用了处理器是 32 位( 4 字节 ),在 64 位处理器上,您只需要在方法中替换 4 x 8。
public static unsafe void CopyUnsafe(byte[] sourceArray, int sourceIndex, byte[] destinationArray, int destinationIndex, int length)
{
const int procInstrSize = 4;
fixed (byte* pDst = &destinationArray[destinationIndex])
{
fixed (byte* source = &sourceArray[sourceIndex])
{
byte* ps = source;
byte* pd = pDst;
// Loop over the count in blocks of 4 bytes, copying an integer (4 bytes) at a time:
for (int i = 0; i < length / procInstrSize; i++)
{
*((int*) pd) = *((int*) ps);
pd += procInstrSize;
ps += procInstrSize;
}
// Complete the copy by moving any bytes that weren't moved in blocks of 4:
for (int i = 0; i < length % procInstrSize; i++)
{
*pd = *ps;
pd++;
ps++;
}
}
}
}