0

I want to call methods inside a loop until a condition is met ( reps >= 1000) and then jump outside of the loop and test which method was more efficient, then jump back into the old method and ignore the method call that was least efficient. Is this possible? Here is my sample code:

// Set the number of times each method is called
public void setReps(String target, String[] sa, long reps) {
    shuffleArray(sa); // Shuffle the order of Strings in array

    for (int i = 0; i < reps; i++) { 
    linearStringSearch(target, sa); 
    backwardLinearStringSearch(target, sa);
    counter(); // counts winner

    if (reps >= 1000)
    chooseAlgorithm(target, sa);
}

So for the above code, I will be testing linearStringSearch() and backwardLinearStringSearch() to see which is more efficient after 1000 loops and then I want to jump back in and ignore either linearStringSearch() or backwardLinearStringSearch() based on the result. I could write a new loop in the chooseAlgorithm() method but I'd prefer to jump back into the old one if possible.

LooMeenin
  • 818
  • 3
  • 17
  • 33
  • 4
    I might use a [Strategy pattern](http://en.wikipedia.org/wiki/Strategy_pattern). You can define a "stringSearch" method in an interface, implement it with `linear` and `reverse linear` search(s)... then write another implementation that tests (a), (b) before choosing one. – Elliott Frisch Jan 25 '14 at 01:30

3 Answers3

2

One approach is to use a Strategy pattern. You can define a "stringSearch" method in an interface, implement it with linear and reverse linear search(s)... then write another implementation that tests (a), (b) before choosing one (perhaps by storing a reference to the "correct" strategy after it "initializes"). You might need to perform a few different iterations of (a), (b) to account for any "JVM warm-up" period.

UML for Strategy Pattern

Community
  • 1
  • 1
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
2

No need to complicate this.

// Set the number of times each method is called
public void setReps(String target, String[] sa, long reps) 
{
    boolean ChosenFastest = false;
    boolean ForwardsIsfaster = false;

    shuffleArray(sa); // Shuffle the order of Strings in array

    for (int i = 0; i < reps; i++) 
    { 
       if(ChosenFastest){
          if(ForwardsIsFaster)
            linearStringSearch(target, sa); 
          else
            backwardLinearStringSearch(target, sa);
       } else {
         linearStringSearch(target, sa); 
         backwardLinearStringSearch(target, sa);
         counter(); // counts winner
       } 

       if (reps == 1000)
          ChosenFastest = true;
          if(ForwardsWasFastest()) //similar to choose algorithm
            ForwardsIsfaster = true;
          }
       }
    }
}
user3125280
  • 2,779
  • 1
  • 14
  • 23
1

Use continue; inside the loop

example:

for (int x = 0; x > 10; x++) {
    if(x > 4){
        continue;
    }
    System.out.println("" + x);
}

The output will never get to 5 because of the continue;

Source: Java Continue statement

For jumping back in, You could have an if statement inside the loop where 0 = algorithm not chosen, 1 = linear, 2 = backwardLinear.

for (int i = 0; i < reps; i++) { 
    if(chosenAlgo == 0) {
        linearStringSearch(target, sa);
        backwardLinearStringSearch(target, sa);
    if(chosenAlgo == 1) {
        linearStringSearch(target, sa);
    }
    else if(chosenAlgo == 2){
        backwardLinearStringSearch(target, sa);
    }
    counter(); // counts winner
}
Glollum
  • 31
  • 6