[H-GEN] Which is better?
Anthony Towns
aj at azure.humbug.org.au
Mon Apr 26 01:53:05 EDT 2004
On Sat, Apr 24, 2004 at 06:12:54PM +1000, Harry Phillips wrote:
> First code:
> int arrayLen = myArray.length;
> for (int i = 0; i < arrayLen; i++)
> ...
>
> Second one:
> for (int i = 0; i < myArray.length; i++)
> ...
Of course, if you're into micro-optimisation, then you really should be
writing:
for (int i = myArray.length; i != 0; i--)
{
...
}
since comparisons against 0 are often much faster than comparisons
against actual numbers; and this way you don't have to worry about the
other variable at all during the loop.
But, then, if you're worrying about micro-optimisations, you need to
be profiling anyway, in which case you might well find that the Java VM
makes many of these optimisations either ineffective or redundant.
> I think the first would be more efficent since it does not have to
> calculate the length of array "myArray" for each loop *then* make a
> comparison to i. The first code only calculates the length once then makes
> one comparision for each loop.
In particular, it doesn't matter what you "think" -- compilers are
complicated enough these days that thinking about things like this isn't
going to be much better than flipping a coin, even if you're an expert
on compiler design. The compiler/VM could well do the exact optimisation
you're thinking of itself, eg, or it could make access to a field in
a class just as efficient as access to a regular variable. Or it could
optimise for memory efficiency and do away with your extra variable and
pretend you'd just written the myArray.length version, after determining
that it didn't change during the loop.
> First code:
> int upperLimit = doCalcs(variable);
> for (i = 0; i < upperLimit; i++) {
> ....
> }
> Second code:
> for (i = 0; i < doCalcs(variable); i++) {
> ....
> }
This can make a real difference since there's generally no reasonable way
for the VM/compiler to decide that doCalcs() won't returning something
different during the course of your loop; and even where there is,
compilers often decide not to optimise it out under the assumption that
you probably wrote it that way for a reason.
Counting backwards makes a difference here too, of course:
for (i = doCalcs(variable); i != 0; i--) {
....
}
> Or is Java more cleverer than I think and it knows that it will have to
> use the calulation multiple times, does the calculation once (using the
> method doCalcs) and uses an internal reference?
Consider if doCalcs() was something like (my Java's a bit rusty):
class foo {
static int i = 0;
static int doCalcs(int v) {
i = i + 3;
return v - i;
}
}
doCalcs(10) will return 7 on the first invocation, then 4, then 1,
then -2, and so on.
Cheers,
aj
--
Anthony Towns <aj at humbug.org.au> <http://azure.humbug.org.au/~aj/>
Don't assume I speak for anyone but myself. GPG signed mail preferred.
Protect Open Source in Australia from over-reaching changes to IP law
http://www.petitiononline.com/auftaip/ & http://www.linux.org.au/fta/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 307 bytes
Desc: Digital signature
URL: <http://lists.humbug.org.au/pipermail/general/attachments/20040426/f902a6f2/attachment.sig>
More information about the General
mailing list