문제

I have an array initialization based on an implied do loop, given an odd size N.

J=(N+1)/2
XLOC(1:N) = (/ (I-J, I=1,N) /)

In the context of F90+ is it recommended to use the (/ .. /) syntax, or is more efficient to use a FORALL statement.

Example: for N=19 then XLOC=(-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9)

How else would you initialize this array?


Edit 1

How would you initialize this array with more readable code?

도움이 되었습니까?

해결책

For such a simple construct both are likely to lead to the same code because compilers are good at optimizing. The FORALL statement is not so much a looping statement but an initialization statement that has many restrictions that can inhibit optimizations. If a simple loop will work, I'd use it.

Also see this previous answer: Do Fortran 95 constructs such as WHERE, FORALL and SPREAD generally result in faster parallel code?

다른 팁

There is no reason they should be less efficient that actual do loops. If you find a case, where they are, report it as an missed optimization bug to your compiler vendor!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top