隐含的 DO 循环是否效率低下?
我有一个基于隐含 do 循环的数组初始化,给定奇数大小 N
。
J=(N+1)/2
XLOC(1:N) = (/ (I-J, I=1,N) /)
在 F90+ 的上下文中,建议使用 (/ .. /) 语法,或者使用 FORALL
语句更有效。
示例:对于 N=19
则 XLOC=(-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1 ,2,3,4,5,6,7,8,9)
你还能如何初始化这个数组呢?
编辑 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于这样一个简单的构造,两者可能会产生相同的代码,因为编译器擅长优化。 FORALL 语句与其说是循环语句,不如说是初始化语句,它具有许多可能抑制优化的限制。如果一个简单的循环可以工作,我会使用它。
另请参阅之前的答案:Fortran 95 结构(例如 WHERE、FORALL 和 SPREAD)通常会产生更快的并行代码吗?
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?
它们没有理由比实际的 do 循环效率低。如果您发现案例(它们在哪里),请将其作为错过的优化错误报告给您的编译器供应商!
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!