fortran77 - Function pointers in fortran -
is possible have function pointers in fortran? right have code lines this:
subroutine flag(iflag,a,b) integer n, a, b, ii, iflag ii = 1, n if (iflag.eq.0) a+b else a-b end return end
the variable "n" has huge value , run code, feel wasting lot of time "if" command. possible write function pointer (i not sure mean that), such @ code this:
subroutine flag(iflag,a,b) *depending on iflag subroutine flag automatically precompiled call either flag_plus or flag_minus* return end subroutine flag_plus(a,b) integer n, a, b, ii ii = 1, n a+b end return end subroutine flag_minus(a,b) integer n, a, b, ii ii = 1, n a-b end return end
if possible can save lot of time avoiding "if" loop. remotely possible?
in fortran 77 can have limited variant of function pointers, namely can pass name of procedure argument procedure (a function pointer, essentially). cannot have variable contains address of procedure , "call" variable, though.
as of fortran 2003, procedure pointer variables part of language.
that being said, think example problem solved easier like
if (iflag == 0) ! why iflag not of type logical? ii = 1, n + b end else ii = 1, n - b end end if
make sure profile see whether has effect, well. modern cpu's have pretty branch predictors, , branch test doesn't change during entire loop pretty best case scenario (rule of thumb: predicted branches close free). heck, compiler might able above kind of optimization..
Comments
Post a Comment