首页 > 解决方案 > Fortran - 变量的使用

问题描述

编译后,使用额外变量引用数组的某些部分与持续索引它在执行速度/效率上是否有任何差异?例如:

... some code that defines/populates "array" here ...
item3 = array(3)
subroutine1(item3, ...)
...
subroutine2(item3, ...)
...
some equation using item3
...
subroutine3(item3, ...)
... etc

相对

... some code that defines/populates "array" here ...
subroutine1(array(3), ...)
...
subroutine2(array(3), ...)
...
some equation using array(3)
...
subroutine3(array(3), ...)
... etc

另一个例子可能是创建一个可以跳过的变量。例如:

x = b / c
y = x + 1  ! This is the only use of x

对比

y = (b / c) + 1

这些在编译时是否等效,或者第一个会因为创建变量“x”而变慢?如果您多次需要“x”的值,那么我认为第一个会更快,因为它可以避免多次进行除法。但只有 1 或 2 次使用,我不确定。

标签: fortran

解决方案


推荐阅读