Try using 1D arrays instead. They are much faster. So, to highlight the matrix N×M do var array = new Int16[N*M]; and access the element (i,j) do array[M*i+j]=...
In my testing, there is a significant improvement over 2D arrays.
If you need, an equally fast way (albeit a bit slower) is to use jagged arrays. You highlight var array = new Int16[N][]; and then for each row array[i] = new Int16[M]; . You access the contents with array[i][j]=...
See a similar answer: fooobar.com/questions/985195 / ...
If your matrices are symmetric, you can speed things up with this fooobar.com/questions/364605 / ...
code
static class Program { [STAThread()] static void Main(string[] args) { const int N=10000; double t1, t2; { var array=new Int16[N, N]; t1=ClockIt(() => { for (int i=0; i<N; i++) { for (int j=0; j<N; j++) { array[i, j]=32767; } } var bytes=new byte[sizeof(Int16)*array.Length]; Buffer.BlockCopy(array, 0, bytes, 0, bytes.Length); Clipboard.SetData(DataFormats.Serializable, bytes); }); } { var array=new Int16[N* N]; t2=ClockIt(() => { for (int i=0; i<N; i++) { for (int j=0; j<N; j++) { array[N*i+j]=32767; } } var bytes=new byte[sizeof(Int16)*array.Length]; Buffer.BlockCopy(array, 0, bytes, 0, bytes.Length); Clipboard.SetData(DataFormats.Serializable, bytes); }); } Console.WriteLine(string.Format("t1={0}, t2={1}",t1, t2)); } public static double ClockIt(this Action test) { var sw=Stopwatch.StartNew(); test(); sw.Stop(); return sw.Elapsed.TotalSeconds; } }
Results (in seconds)
t1=1.110093, t2=0.6908793 (61% faster)
I built the console application in Release mode and ran it from the command window. The results are very consistent. With large arrays, acceleration is much greater.