Tom
Tom

Reputation: 577

C#: Optimizing a large array copy

I need to continuously fill an array of 16384 double elements from a device that is delivering an array of data that is 332 elements in length (these have a data type of short). Currently copying takes 28ms to fill the 16384 element array. I would like to get this under 10ms at least. In the following code the method getData returns two short arrays of 332 elements(iBuf and qBuf). This method takes 14 ticks (3uS) so is not relevant for speed.

        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
            } 

Thanks for any help and suggestions

Upvotes: 1

Views: 710

Answers (3)

MSE
MSE

Reputation: 345

You can use the following technique where we took advantage of the fact that the processor is 32-bit ( 4 Bytes ), on 64-bit processor you just need to replace 4 by 8 in the method.

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++;
            }
        }
    }
}

Upvotes: 0

Mr Dog
Mr Dog

Reputation: 396

Using the Array.copy method might help you

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.
} 

Upvotes: 2

John Alexiou
John Alexiou

Reputation: 29274

First off your code will not compile as is. Try to edit to make a minimum example of what you want done. You are missing initialization (new statements) and it appears like you are writing Java code in C#.

At a minimum use Array.Copy(). Alternatively you can use pointers (if buffers contain intrinsic values) or as mentioned before BlockCopy() which copies bytes. Use the sizeof() function to find how many bytes per element.

Upvotes: 0

Related Questions