Search code examples
performancefunctionoptimizationfortranfortran90

Reduce Fortran function call overhead


I have a Fortran code like this:

file1.f90

program myprog
 use func1mod
    do i=1,N
       call subroutine1
    enddo
    subroutine subroutine1
         integer*8::var1,var2,var3,...
         do j=1,N
            x=func1(var1,var2,var3,..)
            computations based on x
         enddo
    return
    end
end 

file2.f90

 module func1mod
 contains
     func1(var1,var2,var3,....)
         func1=some computations based on var1, var2, var3, ...
      return
     end function func1
 end module func1mod

function func1 does not modify any of its arguments. It computes a value based on the arguments and returns a value. The # of arguments is large but the function is less than 30 lines of code. What is the best approach to reduce the function call overhead. One approach would be to inline the function. Is there any other way out?


Solution

  • The best you can do is be as explicit as possible about the semantics of the function, turn optimization up as high as possible, and let the compiler make the best decision it can about how best to implement the call. Make sure the dummy variables are marked intent(in), and mark the function as pure - although if it's only 30 lines, the compiler will doubtless notice these things anyway at high optimization - and check your compiler options to see if there's anything you can do to encourage (for instance) inlining.