Extreamely fast way to clone the values of an array into a second array?

Posted by George on Stack Overflow See other posts from Stack Overflow or by George
Published on 2011-01-12T15:44:29Z Indexed on 2011/01/12 15:53 UTC
Read the original article Hit count: 161

Filed under:
|

I am currently working on an application that is responsible for calculating random permutations of a multidemensional array.

Currently the bulk of the time in the application is spent copying the array in each iteration (1 million iterations total). On my current system, the entire process takes 50 seconds to complete, 39 of those seconds spent cloning the array.

My array cloning routine is the following:

    public static int[][] CopyArray(this int[][] source)
    {
        int[][] destination = new int[source.Length][];
        // For each Row
        for (int y = 0; y < source.Length; y++)
        {
            // Initialize Array
            destination[y] = new int[source[y].Length];
            // For each Column
            for (int x = 0; x < destination[y].Length; x++)
            {
                destination[y][x] = source[y][x];
            }
        }
        return destination;
    }

Is there any way, safe or unsafe, to achieve the same effect as above, much faster?

© Stack Overflow or respective owner

Related posts about c#

Related posts about arrays