1

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?

andrewmu
  • 14,276
  • 4
  • 39
  • 37
John Alexiou
  • 28,472
  • 11
  • 77
  • 133

2 Answers2

2

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?

Community
  • 1
  • 1
M. S. B.
  • 28,968
  • 2
  • 46
  • 73
2

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!

whoami
  • 86
  • 4