This is an excellent question and also a long standing question and design in Fortran where opinions (still) differ. Read this discussion starting from roughly this comment: Fortran Best Practice Minibook - #8 by certik.
To answer which is faster, the traditional answer is that in the assumed shape (a(:)
) case the compiler passes in an array descriptor, while in the explicit shape (a(n)
) the compiler passes just a pointer and dimension n
as an argument. Depending on how you call it, things might or might not be faster. Also in tight loops the compiler would inline the function so there shouldn’t be any difference.
Conclusion: if performance matters, then the function should be inlined in the loop, in which case both are probably equivalent. If performance does not matter, then the performance might slightly differ, e.g., if you are passing strided arrays, then explicit shape might be faster, if you are passing contiguous arrays that somehow do not have an array descriptor in the caller (such as another subroutine with explicit shape), then the explicit shape might be slightly faster.
Our plan with LFortran is to do things like function inlining at the ASR level, and be able to print it as Fortran source code, so that one can check what exactly the compiler is doing and ensure optimal code is generated (inlined in this case). Currently we decided to pass an array descriptor for all arrays, including explicit shape. We will experiment with different approaches later.