|
|
|
|
|
|
| Author |
Message |
Rhino *nix forums Guru
Joined: 08 Feb 2005
Posts: 449
|
Posted: Thu Feb 24, 2005 11:25 pm Post subject:
Re: Very basic performance question
|
|
|
"Rhino" <rhino1@NOSPAM.sympatico.ca> wrote in message
news:Z1tTd.23664$uO.681321@news20.bellglobal.com...
| Quote: | Given a loop like this:
for (int ix=0; ix<myArray.length; ix++) {
System.out.println(ix);
}
or especially this:
for (int ix=0; ix<Math.sqrt(shoeSize)*hairlength/(IQ); ix++) {
System.out.println(ix);
}
[snip] |
Oops, sorry, wrong newsgroup!
Rhino |
|
| Back to top |
|
 |
Rhino *nix forums Guru
Joined: 08 Feb 2005
Posts: 449
|
Posted: Thu Feb 24, 2005 10:02 pm Post subject:
Very basic performance question
|
|
|
Given a loop like this:
for (int ix=0; ix<myArray.length; ix++) {
System.out.println(ix);
}
or especially this:
for (int ix=0; ix<Math.sqrt(shoeSize)*hairlength/(IQ); ix++) {
System.out.println(ix);
}
I've always assumed that it made more sense to look up or calculate the
value in the second term of the for loop, rather than make Java look it up
or calculate it, especially for large numbers of iterations of the loop. It
just made no sense to do the same calculation or lookup a thousand or a
million times; it seemed a lot more reasonable to do the calculation or
lookup ONCE, store the value in a variable, and then plug the variable into
the for loop, like this:
int loopController = Math.sqrt(shoeSize)*hairlength/(IQ);
for (int ix=0; ix<loopController; ix++) {
System.out.println(ix);
}
Is my reasoning sound or does Java have "tricks" (optimizations) that make
it more sensible to leave the lookup or calculation in the loop, this
avoiding the need to create the variable that controls the loop?
I've been wondering about that for a while and am finally getting around to
asking ;-)
I realize that I could set up a benchmark to find out for sure but I'm
betting that the answer is already well-known so I'd rather ask here and
save myself the work ;-)
--
Rhino
---
rhino1 AT sympatico DOT ca
"There are two ways of constructing a software design. One way is to make it
so simple that there are obviously no deficiencies. And the other way is to
make it so complicated that there are no obvious deficiencies." - C.A.R.
Hoare |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|
|
The time now is Thu Jan 08, 2009 2:10 am | All times are GMT
|
|
Myspace Graphics | Watch Anime Free Online | Credit Score | Best Credit Cards | Debt Consolidation
|
|
Copyright © 2004-2005 DeniX Solutions SRL
|
|
|
|
Other DeniX Solutions sites:
Unix/Linux blog |
electronics forum |
medicine forum |
science forum |
|
|
Privacy Policy
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|