What is faster in C: structures or arrays?

I want to implement (which is abstract) a two-dimensional 4x4 matrix. All the code that I write for matrix multiplication and so on will completely β€œunfold” as it were - that is, I will not use loops to access and write data records in the matrix.

My question is: in C, it would be faster to use the structure as such:

typedef struct { double e0, e1, e2, e3, e4, ..., e15 } My4x4Matrix; 

Or it will be faster:

 typedef double My4x4Matrix[16]; 

Given that I will be accessing each matrix element individually as such:

 My4x4Matrix a,b,c; // (Some initialization of a and b.) ... c.e0=a.e0+b.e0; c.e1=a.e1+b.e1; ... 

or

 My4x4Matrix a,b,c; // (Some initialization of a and b.) ... c[0]=a[0]+b[0]; c[1]=a[1]+b[1]; ... 

Or is it the exact same speed?

+4
source share
4 answers

Any worthy compiler will generate the same code, byte for byte. However, using arrays allows you to greatly increase flexibility; when accessing matrix elements, you can choose whether you want to access fixed locations or addresses with variables.

I also very much doubt your choice to β€œunwind” (expand?) All operations manually. Any good compiler can fully unroll loops with a constant number of iterations for you and can even generate SIMD code and / or optimally plan the order of instructions. It will be harder for you to work better, and you will get code that is disgusting to read. The fact that you asked this question tells me that you are probably not experienced enough to do better than even a naive optimizing compiler.

+15
source

Struct elements (fields) can only be accessed by their names explicitly indicated in the program source, which means that each time you access a field, the actual field must be selected and hard-coded at compile time. If you wanted to implement the same thing with arrays, this would mean that you would use explicit constant indexes of the compile-time array (as in your example). In this case, the performance of the two will be exactly the same, and the generated code will be exactly the same (excluding the "malicious" compilers from consideration).

However, note that arrays provide you with an additional degree of freedom: if necessary, you can select elements of the array with a runtime index. This is not possible for structures. Only you know if this matters to you.

On the other hand, note also that arrays in C cannot be copied, which means that you will be forced to use memcpy to copy My4x4Matrix based on the array. When using structure-based, normal language-level copying will work. Arrays can work around this problem by wrapping the actual array in the structure.

+9
source

I think both have the same speed. The difference between a structure and an array is simply its meaning (from a human point of view). Both will be compiled as memory addresses.

+1
source

I would say that the best way is to create a test to try it yourself. Results may vary depending on system environments and compilers.

+1
source

Source: https://habr.com/ru/post/1333758/


All Articles