I have one pointer array of bytes with length(4325376) and i am trying to copy the values of first array to another one,
but the speed of copying is slow even with pointers: ~50ms
.
The question is: How to increase the speed of copying?
I know there is a copy function to copy the entire array but i don't know if in my case it's possible to use it.
Here is my code:
var mapSource = device.ImmediateContext.MapSubresource(screenTexture, 0, MapMode.Read, SharpDX.Direct3D11.MapFlags.None);
byte[] rgbaValues = new byte[width * height * 4];
var curRowOffs = 0;
var stride = width * 4;
unsafe
{
byte* srcPtr = (byte *)mapSource.DataPointer;
Stopwatch sw = Stopwatch.StartNew();
fixed (byte* pDest = rgbaValues)
{
for (uint y = 0; y < height; y++)
{
var index2 = curRowOffs;
var index = y * mapSource.RowPitch;
for (uint x = 0; x < width; x++)
{
pDest[index2] = srcPtr[index + x * 4 + 0];
pDest[index2 + 1] = srcPtr[index + x * 4 + 1];
pDest[index2 + 2] = srcPtr[index + x * 4 + 2];
pDest[index2 + 3] = srcPtr[index + x * 4 + 3];
index2 += 4;
}
curRowOffs += stride;
}
}
sw.Stop();
Console.WriteLine("{0:N0} Milliseconds", sw.Elapsed.Milliseconds);
}
Udate: The solution was to use Buffer.CopyMemory method, it gave me ~2ms, Awesome!
for (uint y = 0; y < height; y++)
{
System.Buffer.MemoryCopy(srcPtr, destPtr, width * 4, width * 4);
srcPtr = &srcPtr[mapSource.RowPitch];
destPtr = &destPtr[mapDest.Stride];
}