I have a very basic Fortran question as I am a Fortran beginner. I'm using the following function:
real(8) function matchingformulaStk_c(ij,retwealth,iz,ctr,StockReturnM,stkEshare,stkNshare)
IMPLICIT NONE
integer, INTENT(IN) :: ij,iz,ctr
real(8), intent(in) :: StockReturnM,retwealth,stkEshare,stkNshare
real(8) :: R_existing, R_new
real(8), parameter :: p_vest = 0.95d0, match_level = 0.5d0, R401k = 1.02d0
R_existing = stkEshare*StockReturnM+(1d0-stkEshare)*R401k
R_new = stkNshare*StockReturnM+(1d0-stkNshare)*R401k
matchingformulaStk_c = R_existing*retwealth + R_new*ygrid(ij,iz)*((1+match_level*p_vest)*tau_grid(ctr))
end function matchingformulaStk_c
When I profile this function in a larger chunk of code I'm using with Intel VTune, I find that the line R_new = stkNshare*StockReturnM+(1d0-stkNshare)*R401k
takes around 20x in CPU time as long as the line R_existing = stkEshare*StockReturnM+(1d0-stkEshare)*R401k
.
Is there any obvious reason why this could be the case or is this going to be specific to my application? If there are any resources you could point me to as to figuring out how to speed up code hotspots like this one, that would be much appreciated.