0

I have a huge two dimension array. I need to get elements from it, but i have a problem - it's EXTREMLY slow (i think a problem in checking indexes from compiler). How i can get elements of my array in unsafe?

private byte[][] MY_TASTE_ARRAY;

for (int d = 0; d < MANY_TIMES; d++)
{      
   int a = MY_TASTE_ARRAY[first_index][second_index];
   MakeSomethingWitha(a);
}
  • I tried to use List, but problem still here. It's VERY slow. List will be better for me, but i do not understand how i can get pointer in my case – Yuri Schneider Sep 19 '15 at 22:49
  • 1
    1. This is a jagged array (array of arrays) and not a 2-dimensional array (`byte[,]`). 2. You don't need to guess. Use profiler to find the bottleneck. – Dmitry Sep 19 '15 at 22:55
  • Dmitry, sorry, it's jagged. But the problems still here - when i removing acess by index, speed is increasing dramatically. – Yuri Schneider Sep 19 '15 at 23:04
  • With multi processing you can access multiple elements at same time which can improve speed however its not thread safe if you attempt to modify the array or something shared for all processes. – M.kazem Akhgary Sep 19 '15 at 23:07
  • How about a memory stream. The offset of data can easily be calculated and it is still safe. – jdweng Sep 19 '15 at 23:51
  • Depending on your case, you may already be avoiding bounds checks. Check out [this article explaining array bounds check elimination](http://blogs.msdn.com/b/clrcodegeneration/archive/2009/08/13/array-bounds-check-elimination-in-the-clr.aspx), which the compiler can do when it can prove that the index is already within the bounds of the array. Plus, in your example you're only accessing a single element repeatedly--is that actually what you're doing? – 31eee384 Sep 20 '15 at 00:17

1 Answers1

0

Array access is not slow, I tried this program comparing the time of summing a bunch of consecutive recurring numbers from 0 to 255 either directly or putting them in a jagged array or a matrix:

        const int N = 10000;
        var jagged = new byte[ N ][];
        var matrix = new byte[ N, N ];
        var count = (byte)0;
        for (var i=0; i< N;  i++ )
        {
            jagged[ i ] = new byte[ N ];
            for ( var j = 0; j < N; j++ )
            {
                jagged[ i ][ j ] = count;
                matrix[ i, j ] = count;
                count = count == 255 ? 
                    (byte) 0 : 
                    (byte) ( count + 1 );
            }
        }
        var watch = new Stopwatch();

        watch.Start();
        var total = 0;
        for ( var i = 0; i < N; i++ )
        {
            for ( var j = 0; j < N; j++ )
            {
                total += jagged[ i ][ j ];
            }
        }
        watch.Stop();
        Console.WriteLine( "Total with jagged array: {0} in {1} ms", total, watch.ElapsedMilliseconds );

        watch.Restart();
        total = 0;
        count = (byte)0;
        for ( var i = 0; i < N; i++ )
        {
            for ( var j = 0; j < N; j++ )
            {
                total += matrix[ i, j ];
            }
        }
        watch.Stop();
        Console.WriteLine( "Total with matrix: {0} in {1} ms", total, watch.ElapsedMilliseconds );

        watch.Restart();
        total = 0;
        count = (byte)0;
        for ( var i = 0; i < N; i++ )
        {
            for ( var j = 0; j < N; j++ )
            {
                total += count;
                count = count == 255 ?
                    (byte)0 :
                    (byte)( count + 1 );
            }
        }
        watch.Stop();
        Console.WriteLine( "Total without array: {0} in {1} ms", total, watch.ElapsedMilliseconds );

and the results are 497 ms using the jagged array, 624 ms using a matrix and 486 ms computing the numbers directly.

MiMo
  • 11,793
  • 1
  • 33
  • 48
  • The OP did not ask whether using a 2-dimensional/jagged array was slow. – Glen Thomas Sep 20 '15 at 00:04
  • But the OP has an X-Y-Problem. Answering the OP;s question will not really solve his problem.. – MrPaulch Sep 20 '15 at 15:30
  • But your answer does not solve any problem.. It is just a comment on the question. – Glen Thomas Sep 20 '15 at 21:53
  • I think I proved that the problem is somewhere else - hence 'solved' it. Hopefully the OP could try my code and see if he gets similar performance numbers and compare with his actual code – MiMo Sep 20 '15 at 22:09
  • This not only about access to array, but also about all the generated code near it. If you do some 2D mathematical transform on vector, the difference between safe and unsafe may be more that x10 times – jenkas Sep 17 '20 at 20:43