Our goal is to fill a two-dimensional array with 1’s.
using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Running; namespace ToArrays { public class Program { public const int Size = 10000; private static void Main() { BenchmarkRunner.Run<Program>(); } [Benchmark] public void IxJ() { var array = new int[Size, Size]; for (var i = 0; i < Size; i++) { for (var j = 0; j < Size; j++) { array[i, j] = 1; } } } [Benchmark] public void JxI() { var array = new int[Size, Size]; for (var i = 0; i < Size; i++) { for (var j = 0; j < Size; j++) { array[j, i] = 1; } } } } }
Could you say which version is faster and why?