I am writing a Fortran program which involves a very large number of exponential function calls. I found that when the argument of exp() function is a variable, the calculation speed is more than 20 times slower than using constant value as the function argument. Eg, in the following two sample programs, program A is much slower than program B.
program A
real a,b
integer i
a=1.234
do i=1,100000000
b=exp(a)
end do
stop
end program A
=====================
program B
integer i
real b
do i=1,100000000
b=exp(1.234)
end do
stop
end program B
When using variable as the exp() function argument is unavoidable, how can I improve the efficiency of doing exp() calculations?
Some compilers can evaluate intrinsic functions applied to constants at compile time. Thus there is no run time cost to evaluating the intrinsic function in this case. Obviously this can’t be done for true variables since the values won’t be known until runtime.