If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: Quick Array Copy

Thanks for the replies. This is in C# so slightly preferred in that language/.NET, but open to anything that works. The copy is doing it pixel value by pixel value for a camera and is taking over 90 seconds. It would be great to get the copy/convert down to 2 seconds or less if possible (or somehow use the original array in the created bitmap). The total size is 8 megapixels. I am getting the source/original int[,] array data from the camera driver. The UInt16 is needed (or so I think) to create the bitmap used for display in the image control. It would be great if I could find a way to use the int[,] array directly without having to convert/copy it to the UInt16 array. The prototype code looks like:

//Loop that transfers "int" camera data in 2d array (from camera) to "UInt16" array. UInt16 array is needed for the bitmap used by the image control for display.
//Loop (most details of loop not shown)
...
//Core line in loop that copies/converts 1 camera value from the int[,] array to the UInt16[] array.
img2Array[i] = (UInt16)(imgArray[iYPosition, iXPosition]
...

Re: Quick Array Copy

Maybe you could implement only this part in a C DLL. It will probably be faster than anything you can achieve with .NET. You could also make use of SSE.
By the way, I'm wondering why do you have to convert from int to Uint16?

Re: Quick Array Copy

UInt16 is used since I know it works in the Bitmapsource derived object for display within an image control (WPF). The very prototype code is as follows. This is very rough just wanted to get something that would convert the data from one source array to the destination. The 90+ seconds is in debug mode.
int i = 0;
int ImageSize= CameraXSize * CameraYSize;
while (i < ImageSize)
{
iXPosition = 0;
//Go across row doing copy/conversion
while (iXPosition < CameraXSize)
{
img2Array[i] = (UInt16)(imgArray[iYPosition, iXPosition]);//Note: x and y position appear reversed for preloaded array from driver
i++;
iXPosition++;
}
//Increment row
iYPosition++;
}

Re: Quick Array Copy

Wow this sounds like real slow performance times. You may want to look at using some multi-threaded approaches. Computers take advantage of multiple cores and a multi-threaded approach is key here. In any case, sometimes at the very least decreasing image resolution may not degrade user experience significantly in every case.

Re: Quick Array Copy

I suspect that the problem is caused by all the bounds-checking which is being performed on your array accesses - though this wouldn't explain why Release is slower than Debug, that's just weird

To speed this code up you could try to get the compiler to realise that you will never try to access an out-of-bounds element. Here is a simple example. Firstly, this code will perform bounds-checking:

shouldn't, as the compiler will be able to see that the values of i will never be out of bounds so it can optimize out the bounds-checking (NB: this is of course subject to change with future compiler improvements).

If you can rewrite your code so the compiler can perform this optimization it should improve performance. But given that you have 2 arrays, one of which is 1D and one which is 2D, I can't see how you could use this approach here.

If all else fails your best chance might be to use pointers. When you access an array through a pointer there will be no bounds-checking. However, you have to be very careful when doing this. Also, you can only use pointers in 'unsafe' sections of code, and the entire application must be marked as 'unsafe'.

This will be as fast as you can make this loop (barring minor optimizations). If this still isn't fast enough then the original problem wasn't in this code after all - and in this case I would recommend going back to the 'safe' version.