For-loop optimization question
- From: Nino <nino9stars@xxxxxxxxx>
- Date: Tue, 31 Jul 2007 09:33:38 -0700
Hello everyone,
This may seem like a pretty simple question, but one that I'm curious
about. I have 2 nested for-loops in my code, and want to know which is
the best (fastest) way to organize them:
OPTION 1:
for (int x=0; x < (somewhere between 0-40); x++) {
for (int y=0; y < (somewhere between 400-1000); y++) {
<if the x array matches the y array, call some functions and
do some more logic>
<else keep going through loop>
}
}
With option 1, it makes the most sense for me to use with the logic
that needs to be run when an array match is found.
OPTION 2:
for (int x=0; x < (somewhere between 400-1000); x++) {
for (int y=0; y < (somewhere between 1-40); y++) {
<if the x array matches the y array, call some functions and
do some more logic>
<else keep going through loop>
}
}
I believe option 2 will be faster (right?), but there might be a
little more logic that goes into the work when finding a match.
The ultimate question is... How much difference does it really make
(with this example or in general) in the way nested for-loops are
organized and handled in Java?
Let me know if there is anything I'm leaving out that might help, and
thanks in advance!
Nino Skilj
.
- Follow-Ups:
- Re: For-loop optimization question
- From: Roedy Green
- Re: For-loop optimization question
- Prev by Date: Re: rant: why is it acceptable to be this... lazy?
- Next by Date: Re: Need efficient search strategy in list of time intervals
- Previous by thread: Generate serialVersionUID in Eclipse for serializable object
- Next by thread: Re: For-loop optimization question
- Index(es):
Relevant Pages
|
|