when optimizing my code, i find myself often writing something like the following ...
do i = 1,n
r = t(i)
y(i) = r*r*2.0
f(i) = r*3.5
enddo
what i am doing with my variable, r
is really irrelavent except that it is used more than once, and is most likely used as many as 5 or 6 times. now, in my head, i do this because i figure that accessing an array is slow and i want to store that variable so that i can retrieve it faster the next time. is this a correct line of thinking though? i wonder if, since i have used t(i), it is just as easy to reference in short succession as it is to reference r
again. does storing a value from an array into a temporary variable make it quicker to us that value again, or is it just the same as if i had written
do i = 1,n
r = t(i)
y(i) = t(i)*t(i)*2.0
f(i) = t(i)*3.5
enddo
? if they are the same, then i am actually slowing things down by writing an extra assignment (r = t(i)
).
time
command and the poor man's profiler are also useful. $\endgroup$