Are implied DO loops inefficient?

浪子不回头ぞ 提交于 2019-12-11 13:33:26

问题


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?


回答1:


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?




回答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!



来源:https://stackoverflow.com/questions/4563918/are-implied-do-loops-inefficient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!