1

In Fortran, we are allowed to create a derived data type:

type vig_
    double precision :: L
    double precision :: D_i
    double precision :: D_o
end type vig_

type(vig_) :: vig

My question is about performance when using derived data types.

I can create an array of the derived type:

type vig1_
    double precision :: L
    double precision :: D_i
    double precision :: D_o
end type vig1_

type(vig1_), allocatable :: vig1(:)

! ...
allocate(vig1(n))
! ...

or a derived data type of arrays:

type vig2_
    double precision, allocatable :: L(:)
    double precision, allocatable :: D_i(:)
    double precision, allocatable :: D_o(:)
end type vig2_

type(vig2_) :: vig2

! ...
allocate( vig2%L(n) , vig2%D_i(n) , vig2%D_o(n) )
! ...

Of course there is a difference, but what exactly is the difference between both approaches? Which is faster? If I want to call a subroutine with these values, which performs better?

! option 1
do k=1,n
    call foo1( vig1(k) )
end do

! option 2
do k=1,n
    call foo2( vig2%L(k) , vig2%D_i(k) , vig2%D_o(k) )
end do

subroutine foo1(vig)
    type(vig1_) :: vig
end subroutine foo1

subroutine foo2(L,D_i,D_o)
    double precision :: L, D_i, D_o
end subroutine foo2
5
  • This is a well-known problem with loads of literature available mathworks.com/videos/… Commented Apr 15, 2019 at 21:49
  • Possible duplicate stackoverflow.com/questions/11616941/… stackoverflow.com/questions/1125626/… stackoverflow.com/questions/17924705/… Commented Apr 15, 2019 at 21:54
  • 2
    @VladimirF would you have duplicates in Fortran? It seems fair to expect a Fortran-specific answer for such a performance problem, isn't it? Commented Apr 16, 2019 at 6:25
  • @PierredeBuyl That's why I didn't close it single-handedly, but to be fair, I think it is pretty much the same in all compiled languages. There is nothing I would add to those links and I would have expected some prior research from the OP - but I did not downvote myself. Commented Apr 16, 2019 at 8:18
  • Fair enough :-) Maybe it can stay as a duplicate, enhancing the discoverability for people looking specifically into Fortran? Commented Apr 16, 2019 at 14:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.