我需要从一个设备连续填充一个包含16384个双精度元素的数组,该设备提供的数据数组长度为332个元素(这些元素的数据类型为short)。目前,复制需要28ms来填充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
}
感谢您的帮助和建议
发布于 2016-05-22 15:11:18
使用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.
}
发布于 2016-05-22 15:00:50
首先,你的代码不会按原样编译。尝试编辑,使你想要做的事情成为最小的例子。您缺少初始化(Java语句),并且看起来像是在用C#编写new
代码。
至少使用Array.Copy()
。或者,您可以使用指针(如果缓冲区包含内部值),或者像前面提到的复制bytes
的BlockCopy()
。使用sizeof()
函数来确定每个元素的字节数。
发布于 2016-07-27 21:11:24
您可以使用以下技术,其中我们利用了处理器是32位(4字节)的事实,在64位处理器上,您只需在方法中将4替换为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++;
}
}
}
}
https://stackoverflow.com/questions/37370206
复制相似问题