【问题标题】:Can I test for a condition inside a for-loop and if it is met, ignore a method call inside the loop?我可以测试 for 循环内的条件,如果满足,忽略循环内的方法调用吗?
【发布时间】:2014-01-25 01:25:24
【问题描述】:

我想在循环中调用方法,直到满足条件(reps >= 1000),然后跳出循环并测试哪个方法更有效,然后跳回旧方法并忽略该方法调用效率最低。这可能吗?这是我的示例代码:

// 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);
}

因此,对于上面的代码,我将测试 linearStringSearch() 和 backwardLinearStringSearch() 以查看在 1000 次循环后哪个更有效,然后我想跳回去忽略 linearStringSearch() 或 backwardLinearStringSearch() 基于结果。我可以在 chooseAlgorithm() 方法中编写一个新循环,但如果可能的话,我更愿意跳回旧循环。

【问题讨论】:

  • 我可能会使用Strategy pattern。您可以在接口中定义“stringSearch”方法,使用linearreverse linear search(s) 实现它...然后编写另一个实现来测试 (a)、(b),然后再选择一个。
  • 看起来不错。我试试看

标签: java for-loop


【解决方案1】:

无需复杂化。

// 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;
          }
       }
    }
}

【讨论】:

  • Java 没有类型 bool。并且那些不遵循 Java 样式名称。
【解决方案2】:

一种方法是使用Strategy pattern。 您可以在接口中定义“stringSearch”方法,使用线性和反向线性搜索来实现它......然后编写另一个实现来测试(a),(b),然后再选择一个(可能通过存储对“初始化”后的“正确”策略)。您可能需要对 (a)、(b) 执行几次不同的迭代,以说明任何“JVM warm-up”期间。

【讨论】:

    【解决方案3】:

    在循环中使用继续;

    示例:

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

    因为继续,输出永远不会达到5;

    来源:Java Continue statement

    为了跳回,您可以在循环中添加一个 if 语句,其中 0 = 未选择算法,1 = 线性,2 = 后向线性。

    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
    }
    

    【讨论】:

      【解决方案4】:

      一种方法是您可以使用两个 for 循环并在 java 中使用带标签的 continue 语句。

      因为有标签的 continue 语句会跳过带有给定标签的外循环的当前迭代。

      来源和参考 - continue example

      【讨论】:

        猜你喜欢
        • 2016-03-18
        • 2021-08-22
        • 2017-04-29
        • 2022-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多