I'm at a performance bottleneck in my program where I need to access elements from an array millions of times in a tight loop.
I looked around and the general consensus seems to be that even though multidimensional arrays should be faster, their underlying implementation is inefficient so just use jagged arrays instead. I profiled it and sure enough, jagged arrays are 50% faster. Fine.
However, I also tried just doing manual indexing (as in, simulating the behavior of a multidimensional array by just doing something like this: object value = array[i * 24 + j]; (where 24 is an array size)
and accessing it through a single-dimension array with multiplication to simulate multidimensional arrays.
Surprisingly, this was also faster than a jagged array by around 15% for accesses (all I care about). This saddens me because for one, it seems silly that manually recreating multidimensional arrays is this much faster than C#'s built-in implementation and two, the math involved to get to indicies is uglier compared to just indexing with jagged/multidimensional arrays.
Is there anything I can do to recoup the speed benefits without having to use my own manual indexing? Surely there is some sort of optimization that can be set or checked to simulate this behavior? Why is the C# implementation of arrays so inefficient?