I am translating my Fortran code to Julia.
From what I learned from Julia guys, they told me by all means prevent using global variables. Using global variables slow down the code.
I wonder, is it true in Fortran?
For example, I usually put some subroutines in a module, and at the beginning of the module, I have many global variables, like, below
module Mod
use constants
use mixture
use formats
implicit none
real(kind=r8), private, allocatable, save :: Yji(:,:),t(:)
integer(kind=i8), private, save :: imode,iter,itermax &
,msample,mgauss,mgauss_tot,mgauss_max,mgauss_min,nsub,mi,mitot,dim_p,kmix
real(kind=r8), private, save :: tsum,mu1,sig1,mu2,sig2,muV,SigV,w1,w2,sig,sig_inv,sigsq2_inv,D &
,normpdf_factor_pYq_i,log_normpdf_factor_pYq_i,sigma_factor
real(kind=r8), private, allocatable, save :: LL_iter(:),muk_iter(:,:),sigk_iter(:,:) &
,muV_iter(:,:),sigV_iter(:,:),wk_iter(:,:),sigma_iter(:) &
,mukerror_iter(:,:),sigkerror_iter(:,:) &
,muVerror_iter(:,:),sigVerror_iter(:,:),sigmaerror_iter(:) &
,ar_gik(:),ar_gik_k(:,:) &
,thetas(:,:,:),thetaspr(:,:,:) &
,norm(:),norm_sig(:),normerr(:),log_norm(:) &
,pYq_ikm(:,:,:),hi_diag(:,:,:,:),yimhi(:,:,:,:) &
,pYq_km(:,:),log_pYq_km(:,:),h_diag(:,:,:),ymh(:,:,:) &
,pYq_ikm_o(:,:,:)
real(kind=r8), private, allocatable, save ::wnow(:),log_wnow(:),nik(:,:),nik_sig(:,:),wknik(:,:),wknik_sig(:,:),tauik(:,:)
integer(kind=i8), private, allocatable, save :: isub_Metroplis_gik_all(:,:),mgauss_ik(:,:)
contains
subroutine init(...)
...
end subroutine init
subroutine calc(...)
...
end subroutine calc
...
end module Mod
I storage many variables and arrays as global(private) in the module. I wonder, is this a bad habbit?
Do you guys also prevent using global variables as best as you can, and put those stuff in the arguments of the subroutine instead?
If I put everything in the arguments of a subroutine, especially many arrays, will that create many unnecessary temp arrays which involve many unnecessary memory operations which can slow down the code?
Thanks in advance!
All those with