I was disappointed to find that Array.Clone, Array.CopyTo, and Array.Copy can all be beat by a simple 3-line manually coded for(;;) loop:
for(int i = 0; i < array.Length; i++)
{
retval[i] = array[i];
}
Say the base case for performing an operation a few million times on an array of some predetermined size takes 10 seconds.
Using Array.Clone before each operation is abysmal extending that time to 100 seconds.
Using Array.CopyTo and Array.Copy takes only about 45 seconds.
The loop above takes only about 20 seconds.
(Forget the subjective argument of whether this makes a difference in the real world, because in the real world it's arguably just as simple to code a 3-line for(;;) loop as to look up the documentation for the Array class.)
I'm just wondering why the performance is so much different. In good old C, the simplest library function, memcpy() performs about the same as a manual for(;;) loop like the one above, and I'm wondering if there's some other array copy function in .NET that's implemented as such a nice simple for(;;) without whatever abstractions are getting in the way of Array.Clone, Array.CopyTo, and Array.Copy.