fast copy from double[] to double[,]

B

barker7

I use a simple double[] array plus a variable to store the row size to
represent two dimensional data. I need to quickly copy this data to a
two dimensional array: double[,].

Currently I iterate through the double[], one by one and place it into
the preallocated double[,]. The arrays are large, and I'm doing a lot
of these. Thus it has become a significant bottleneck.

Can anyone suggest a faster way to convert a double[] + row size info
to a double[,]. I'm hoping there is some way to use one of the
Array.Copy() methods to copy at least a whole row at a time.

Thanks,

Mitch
 
L

Laura T.

Buffer.Copy is the fastest method I know for matrix transformation.

A snippet how I use it:

private static double[,] DoubleArrayMatrix(double[] arrSrc,int
rowLength)
{
double[,] arrDst = new double[arrSrc.Length/rowLength,
rowLength];
Buffer.BlockCopy(arrSrc, 0, arrDst, 0, arrSrc.Length *
sizeof(double));
return arrDst;
}
 
B

barker7

That's great..

Buffer.BlockCopy is about 5x faster than copying each value
individually. (My test was on a array size of 2000x5000 row x col)

Thanks Laura.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top