Both representations can be efficient, but the real performance difference depends on the specifics of your use-case.
In .net a bool is stored as an uncompressed 1-byte value, while an int occupies 4 bytes and is naturally aligned. This means the arrays will have different memory footprints, and performance will mostly be determined by how your algorithm accesses them: cache locality, access patterns, alignment, and branch behavior all matter far more than the nominal cost of loading a single element.
Another practical factor is SIMD: the .net JIT can auto-vectorize operations on int arrays (and on bit-packed data such as uint[]/ulong[]) using SSE/AVX instructions. Choosing a type that cannot be efficiently vectorized may lead to a substantial performance penalty.
For purely binary data (on/off pixels), the fastest approach in practice is often a bit-packed format uint[] (and ulong[] for x64), because it minimizes memory traffic and lets you process many pixels at once using bit operations.
Between the two specific options:
- bool[30][832] uses less memory, which may increase performance by allowing more of the array to fit in the CPU cache, but access patterns can be less predictable, potentially reducing throughput in tight loops;
- int[30][832] uses more memory but benefits from alignment and simpler address calculations.
Which one is “faster” depends entirely on how the data is traversed and manipulated in your image-processing code.
For better performance, I would recommend avoiding jagged or multidimensional arrays for this scenario. Instead, use a single contiguous one-dimensional array, such as uint[30 * 832].
There are several reasons for this:
- Cache locality: A contiguous block of memory allows more predictable access patterns and better cache utilization, which can significantly improve performance.
- Reduced memory fragmentation: Jagged arrays (T[][]) or multidimensional arrays may allocate separate memory blocks for each row, increasing fragmentation, reducing throughput, and adding pressure on the GC.
- Simpler indexing and SIMD optimization: Accessing elements with array[row * width + col] is faster than nested indexing and allows the JIT to potentially vectorize operations more effectively.
This approach is especially beneficial for binary on/off data, where memory bandwidth and tight loops dominate performance.