M
mrTriffid
Hi,
I'm interested in finding out something that's been bugging me for a
while now; does it make sense to optimize the following (idealised)
for-loop
for (int i = 0; i < foo.getBar(); i++)
into something like
for (int i = foo.getBar()-1; i >= 0; i--)
(assuming foo.getBar() doesn't change for the duration of the loop)?
In the first case, foo.getBar() is (theoretically) evaluated many
times, but only once in the second.
Ignoring the fact that the Java spec probably says nothing about such
optimizations, does it make sense to assume that (say) the vanilla Sun
JDK compiler would spot this, and would optimize the first loop to be
as good as the second?
Thanks in advance!
I'm interested in finding out something that's been bugging me for a
while now; does it make sense to optimize the following (idealised)
for-loop
for (int i = 0; i < foo.getBar(); i++)
into something like
for (int i = foo.getBar()-1; i >= 0; i--)
(assuming foo.getBar() doesn't change for the duration of the loop)?
In the first case, foo.getBar() is (theoretically) evaluated many
times, but only once in the second.
Ignoring the fact that the Java spec probably says nothing about such
optimizations, does it make sense to assume that (say) the vanilla Sun
JDK compiler would spot this, and would optimize the first loop to be
as good as the second?
Thanks in advance!