Is there an efficient way of copying an array of nullables (say byte?[]
) to an array of non-nullables (say byte[]
) assuming that the source array is guaranteed to not have any nullables (and if it does, it's ok to throw an exception)? Obviously, I can loop over the indices and copy each element individually.
This does not work. It compiles, but throws an ArrayTypeMismatchException
at run-time.
byte?[] sourceNullable = new byte?[]{1,2,3};
byte[] destNonNullable = new byte[3];
Array.Copy(sourceNullable,destNonNullable,3);
This will work but I am looking for something "better"
for(int i=0;i<3;i++) {
destNonNullable[i] = sourceNullable[i] ?? 0;
}
I'm willing to accept the answer: what's wrong with the explicit loop? And why are you wasting time optimizing this? :)
Edit: I tried using the Linq style Cast<>()
, but that turns out to be much slower. The time summary from my code below:
for loop = 585 ms
Linq Cast = 3626 ms
The input image
file is a sparse array, filled with sections of nulls.
uint rowsize = 16;
Stopwatch sw = new Stopwatch();
sw.Start();
for (UInt32 address = start & 0xFFFFFFF0; address <= last; address += rowsize)
{
Int32 imageOffset = (Int32)(address - start);
Int32 maxRowLen = (int)rowsize;
if (maxRowLen + imageOffset > image.Length) maxRowLen = (image.Length - imageOffset);
if (maxRowLen == 0) throw new Exception("this should not happen");
int ptr = 0;
while (ptr < maxRowLen)
{
while (ptr < maxRowLen && image[imageOffset + ptr] == null) ptr++;
int startOffset = ptr;
while (ptr < maxRowLen && image[imageOffset + ptr] != null) ptr++;
int stopOffset = ptr;
if (startOffset < maxRowLen)
{
#if false
int n = stopOffset - startOffset;
byte[] subdata = new byte[n];
for (int i = 0; i < n; i++)
{
subdata[i] = image[imageOffset + startOffset + i] ?? 0;
}
#else
byte[] subdata = image.Skip(imageOffset + startOffset).Take(stopOffset - startOffset).Cast<byte>().ToArray();
#endif
IntelHexRecord rec = new IntelHexRecord((uint)(address + startOffset), subdata);
records.Add(rec);
}
}
}
sw.Stop();
Console.WriteLine("elapsed: {0} ms", sw.ElapsedMilliseconds);