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.