Search code examples
performancemodulefortran

Does Fortran use module command have higher operation cost if there are more variables in the module?


Would this:

module variables
      ! 100 variables declared here
end

Be significantly more computationally expensive when used everywhere than if there are 5 modules with 20 variables each and only some are called in different places?

In other words, does the use statement iterate through all the contents or does it simply give access in a more efficient way regardless of what the specific contents are?

Thanks!


Solution

  • Fortran programs are (nearly always) compiled and so declaring variables should have no overhead at runtime. Except they take some space in the heap/stack. While this generally have no significant impact on the performance of programs, there are few (very) rare pathological cases. For example, declared variables can impact the the alignment in memory of other variable and the alignment can slightly impact the speed of loading variable from memory. The organization of the variable in modules does not introduce any additional overhead (as long as optimizations are enabled) since Fortran programs are compiled to monolithic binaries. I think it is a good idea not to care about that unless you see regressions. One should focus about readability & maintainability first.