What is the probability that cons<=0?
The pattern is deterministic, so I could add an early exit clause:
! Initialize v_new
v_max = large_negative
ap_ind = 0
! Choose a' optimally by stepping through all possible values
do ap_c=1,n_a
aprime_val = ap_grid(ap_c)
cons = R*a_val + z_val - aprime_val
if (cons>0.0d0) then
v_temp = f_util(cons) + beta*EV(ap_c,z_c)
!v_temp = f_util(cons) + beta*sum(v(ap_c,:)*z_tran(z_c,:))
if (v_temp>v_max) then
v_max = v_temp
ap_ind = ap_c
end if
else
exit
endif
enddo !end a'
I did not do it in the initial version because I read somewhere that introducing exit clauses within nested loops can be bad for performance. Of course the exit clauses saves on the number of iterations on the ap_c loop: it may prevent some optimizations however so the net effect on performance is not clear a priori.