title: Performance hit using modules in fortran
body: <p>I'm converting a F77 code into F90, and i'm trying to get rid of implicit declaration and includes.</p>
  
  <p>In particular, I have a subroutine (frequently called)</p>
  
  <p>the subroutine :</p>
  
  <pre><code>  SUBROUTINE GRADE(NIE,NJE,NKE,IDEW,IDNS,IDTB,FI,FIF,GRX,GRY,GRZ)
    USE module_dec
    USE module_geom_common
    IMPLICIT NONE
    REAL    :: FI(NXYZA),FIF(NXYZA)
    REAL    :: GRX(NXYZA),GRY(NXYZA),GRZ(NXYZA)
    REAL    :: BN(NXYZA),BE(NXYZA),BT(NXYZA)
    REAL    :: FIE
    INTEGER :: NIE,NJE,NKE,IDEW,IDNS,IDTB
    INTEGER :: I,J,K
  
    ! Declaration of the variables used in file.inc
    REAL    :: ARX,ARY,ARZ
    REAL    :: AKX,AKY,AKZ
    REAL    :: ARE,ARE2,ARKSI2
    REAL    :: FXE,FXW
    INTEGER :: INE
    REAL    :: DELN,DELNR
  
    DO 30 K=2,NKE
    DO 30 I=2,NIE
    DO 30 J=2,NJE
  
    INCLUDE "file.inc"
  
      FIE=FI(INP)*FXW+FI(INE)*FXE
      BE(INP)=  FIE*ARX
      BN(INP)=  FIE*ARY
      BT(INP)=  FIE*ARZ
  
    30 CONTINUE
  
    DO 31 K=2,NKE
    DO 31 I=2,NIE
    DO 31 J=2,NJE
  
      INP=LK(K)+LI(I)+J
      GRX(INP)=GRX(INP)+BE(INP)
      GRY(INP)=GRY(INP)+BN(INP)
      GRZ(INP)=GRZ(INP)+BT(INP)
  
      GRX(INP+IDEW)=GRX(INP+IDEW)-BE(INP)
      GRY(INP+IDEW)=GRY(INP+IDEW)-BN(INP)
      GRZ(INP+IDEW)=GRZ(INP+IDEW)-BT(INP)
  
    31 CONTINUE
  </code></pre>
  
  <p>and file.inc</p>
  
  <pre><code>  INP=LK(K)+LI(I)+J
    INE=INP+IDEW
    INS=INP-IDNS
    INB=INP-IDTB
    INBS=INB-IDNS
  
    FXE=FIF(INP)
    FXW=1.-FXE
  
    DXS=.5*(X(INP)-X(INS)+X(INB)-X(INBS))
    DYS=.5*(Y(INP)-Y(INS)+Y(INB)-Y(INBS))
    DZS=.5*(Z(INP)-Z(INS)+Z(INB)-Z(INBS))
  
    DXT=.5*(X(INP)-X(INB)+X(INS)-X(INBS))
    DYT=.5*(Y(INP)-Y(INB)+Y(INS)-Y(INBS))
    DZT=.5*(Z(INP)-Z(INB)+Z(INS)-Z(INBS))
  
    ARX=DYS*DZT-DYT*DZS
    ARY=DZS*DXT-DZT*DXS
    ARZ=DXS*DYT-DXT*DYS
  
    ARE2=ARX**2+ARY**2+ARZ**2
  
    AKX=XC(INE)-XC(INP)
    AKY=YC(INE)-YC(INP)
    AKZ=ZC(INE)-ZC(INP)
    ARKSI2=AKX**2+AKY**2+AKZ**2
  </code></pre>
  
  <p>With wich, my program have an execution time of 12s</p>
  
  <p>When I move the declaration of variables present in 'file.inc' in a module,</p>
  
  <pre><code>  MODULE module_geom_common
      REAL    :: ARX,ARY,ARZ
      REAL    :: AKX,AKY,AKZ
      REAL    :: ARE,ARE2,ARKSI2
      REAL    :: FXE,FXW
      INTEGER :: INE
      REAL    :: DELN,DELNR
    END MODULE module_geom_common
  </code></pre>
  
  <p>I have now an execution time of 13s.</p>
  
  <p>I'm compiling with ifort, but I observe the same comportment with gfortran.</p>
  
  <p>I don't understand why am I slower.
  Is it because the variables declared inside a module will be put elsewere in memory, causing cache miss or something like that ?</p>
  
  <p>Is there a way to overcome this without using include ?</p>
  
  <p>EDIT : I've edited my question in order to give more details.</p>