For-loop optimization question



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

.



Relevant Pages

  • RE: Error 3021
    ... Create proto-file names using the selected job names and storre to an array ... Save and close the document and repeat the loop ... Dim strJobsAs String, strDocsAs String, varValsAs _ ...
    (microsoft.public.access.modulesdaovba)
  • RE: Error 3021
    ... Kevin Backmann ... Create proto-file names using the selected job names and storre to an array ... Save and close the document and repeat the loop ... Dim strJobsAs String, strDocsAs String, varValsAs _ ...
    (microsoft.public.access.modulesdaovba)
  • RE: Error 3021
    ... Create proto-file names using the selected job names and storre to an array ... Save and close the document and repeat the loop ... Dim strJobsAs String, strDocsAs String, varValsAs _ ...
    (microsoft.public.access.modulesdaovba)
  • Re: Displaying a large amount of data quickly (VB6)
    ... >>> involving a loop of VB code would be too slow. ... but I'd sure be interested to know if that StringBuilder ... Array elements: 25000 ... Array construction: 17 ...
    (microsoft.public.vb.controls)
  • Re: Problem with a script
    ... a loop there becomes impractical. ... You still have them as uniquely named array indexes... ... writing the code twice will only ... reading your entire code and parsing it in their head, ...
    (comp.lang.php)