Testing different fortran coding paradigms for speed
This test compares looping over individual arraysof variables with multiple dimensions to loopingover arrays of derived types, where e.g., the last dimensionis contained within the derived type.
Case 1 (arrays): a single derived type contains 3D arrays inside it.
Case 2 (columns): a 2D array of derived types contains 1D arrays inside them.
Case 3 (points): a 3D array of derived types contains points inside them.
To perform the test, compile the code, then run it:
gfortran looping_over_types.f90./a.out
Example output:
Time using: arrays: 2.45 sTime using: columns: 2.08 sTime using: points: 1.99 s
However, using different compiler flags can change the comparison:
gfortran -O3 -flto -march=native looping_over_types.f90./a.out
Time using: arrays: 1.90 sTime using: columns: 1.53 sTime using: points: 1.53 s
Conclusions
gfortran 12.2.0, ifort 19.1.3.304
Using arrays inside the derived type is a little slower thanusing a 2D array of derived types with a column vector inside,and the fastest is actually to use a 3D array of derived typeswith points inside.
The right compiler flags can greatly reduce differences between these methods.