Clicky

Fortran Wiki
Assumed shape array discussion

What are the performance considerations of assumed shape arrays?

Examples of the two primary alternatives:

! This is a subroutine using assumed shape array
subroutine test_as(array)
  real, intent(in), dimension(:) :: array

  ! ...
end subroutine test_as

! This is a subroutine using automatic arrays
subroutine test_auto(array, n)
  real, intent(in), dimension(n) :: array
  integer, intent(in) :: n
  ! ...
end subroutine test_auto

One of the major differences is that an assumed-shape array can be “strided”. If you pass an array section, such as the third column in a 2D array, the caller can pass an array reference that points to strided (non-contiguous) data in memory. That can be an advantage, because the source array does not have to be packed into a contiguous temporary array. However, when the routine is called with contiguous arrays, the optimization can be hindered.

Another difference, especially for multi-dimensional arrays. is that a small, fixed inner dimension can be optimized more efficiently. If a coordinate array always has dimension (3,*), there is no need to implement a loop over the 3 inner elements. If declared (:,:), there is a good chance that a loop will be used.