【发布时间】: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”方法,使用
linear和reverse linearsearch(s) 实现它...然后编写另一个实现来测试 (a)、(b),然后再选择一个。 -
看起来不错。我试试看