Search code examples
linqplinq

Two dimensional array sum in C#/Linq


I have a two dimensional array of integers. I would like to write an optimized and fast code to sum all the columns of the two dimensional array.

Any thoughts how I might be able to do this using LINQ/PLINQ/TASK parallelization ?

Ex:

private int[,] m_indexes = new int[6,4]  { {367, 40, 74, 15},
                                           {535, 226, 74, 15}, 
                                           {368, 313, 74, 15},
                                           {197, 316, 74, 15}, 
                                           {27, 226, 74, 15},
                                           {194, 41, 74, 15} };

Solution

  • The simplest parallel implementation:

     int[,] m_indexes = new int[6, 4]  { {367, 40, 74, 15},
                                         {535, 226, 74, 15}, 
                                         {368, 313, 74, 15},
                                         {197, 316, 74, 15}, 
                                         {27, 226, 74, 15},
                                         {194, 41, 74, 15} };
     var columns  = Enumerable.Range(0, 4);
     int[] sums = new int[4];
     Parallel.ForEach(columns, column => {
         int sum = 0;
         for (int i = 0; i < 6; i++) {
             sum += m_indexes[i, column];
         }
                sums[column] = sum;
     });
    

    This code can obviously be "generalized" (use m_indexes.GetLength(0) and m_indexes.GetLength(1)).

    LINQ:

    var sums = columns.Select(
        column => {
            int sum = 0;
            for (int i = 0; i < 6; i++) {
                sum += m_indexes[i, column];
             } return sum; 
        }
    ).ToArray();
    

    Be sure to profile on real-world data here if you truly need to optimize for performance here.

    Also, if you truly care about optimizing for performance, try to load up your array so that you summing across rows. You'll get better locality for cache performance that way.