如何减少重复的代码,这些代码都是相同的,但运行不同的函数?

huangapple go评论142阅读模式
英文:

How should I reduce repetitive code that are all the same but run different functions?

问题

你可以通过创建一个方法,该方法接受一个算法作为参数,然后在不同的循环中调用该方法,并将不同的算法传递给它。这将减少代码的重复性。以下是示例代码:

// 创建一个方法,接受算法作为参数,并返回平均时间
public static long runAlgorithm(YourClass x, int[] randomArray, int algorithmIndex) {
    long total = 0;
    for (int j = 0; j < 5000; j++) {
        if (algorithmIndex == 1) {
            x.algorithm1(randomArray[algorithmIndex]);
        } else if (algorithmIndex == 2) {
            x.algorithm2(randomArray[algorithmIndex]);
        } // 可以根据需要添加更多算法
        long t2 = System.nanoTime();
        long elapsed = t2 - t1;
        total += elapsed;
    }
    return total / 5000;
}

// 在循环中调用方法,并将不同的算法索引传递给它
for (int i = 0; i < 19; i++) {
    long t1 = System.nanoTime();
    matrix[i][0] = runAlgorithm(x, randomArray, 1); // 调用算法1
}

// 在另一个循环中调用方法,传递不同的算法索引
for (int i = 0; i < 19; i++) {
    long t1 = System.nanoTime();
    matrix[i][1] = runAlgorithm(x, randomArray, 2); // 调用算法2
}

// 可以继续在其他循环中调用方法,传递不同的算法索引

通过这种方式,你可以避免重复的代码,并轻松更改要调用的算法。你只需要更新 runAlgorithm 方法的条件语句来处理更多的算法选项。

英文:
  for (int i = 0; i &lt; 19; i++) {
         long t1 = System.nanoTime();
         long total = 0;
         for (int j = 0; j &lt; 5000; j++) {
            x.algorithm1(randomArray[i]);
            long t2 = System.nanoTime();
            long elapsed = t2 - t1;
            total += elapsed;
         }
         long averageTime = 0;
         averageTime= total / 5000;
         matrix[i][0] = averageTime;
      }

  /* Running Algorithm 2 */
  for (int i = 0; i &lt; 19; i++) {
     long t1 = System.nanoTime();
     long total = 0;
     for (int j = 0; j &lt; 5000; j++) {
        x.algorithm2(randomArray[i]);
        long t2 = System.nanoTime();
        long elapsed = t2 - t1;
        total += elapsed;
     }
     long averageTime = 0;
     averageTime = total / 5000;
     matrix[i][1] = averageTime;
  } 

I have 3 of these for loops that basically do the same thing however the line "x.algorithm2(randomArray[i]);" changes to a different algorithm in each different for loop. How can I reduce this repetitive code to only change the algorithm being called?

答案1

得分: 1

这里是使用函数式接口和泛型的示例代码:

public static <T> void runAlgorithm(long[][] matrix, Consumer<T> tConsumer, T[] tArray) {
    for (int i = 0; i < 19; i++) {
        long t1 = System.nanoTime();
        long total = 0;
        for (int j = 0; j < 5000; j++) {
            tConsumer.accept(tArray[i]);
            long t2 = System.nanoTime();
            long elapsed = t2 - t1;
            total += elapsed;
        }
        long averageTime = total / 5000;
        matrix[i][1] = averageTime;
    }
}

public static void main(String[] args) {
    long[][] matrix1 = null; // ???
    Integer[] randomInts = new Integer[] {}; // ???
    runAlgorithm(matrix1, Algorithms::algorithmOnInts, randomInts);

    long[][] matrix2 = null; // ???
    Object[] randomObjects = new Object[] {}; // ???
    runAlgorithm(matrix2, Algorithms::algorithmOnObjects, randomObjects);
}

public static class Algorithms {
    public static void algorithmOnInts(Integer i) {}
    public static void algorithmOnObjects(Object o) {}
}

注意:这里的 ??? 部分是示例代码中的占位符,需要您根据实际情况填写相应的内容。

英文:

Here's an example of what you could do using functional interfaces and generics:

public static &lt;T&gt; void runAlgorithm(long[][] matrix, Consumer&lt;T&gt; tConsumer, T[] tArray) {
	for (int i = 0; i &lt; 19; i++) {
		long t1 = System.nanoTime();
		long total = 0;
		for (int j = 0; j &lt; 5000; j++) {
			tConsumer.accept(tArray[i]);
			long t2 = System.nanoTime();
			long elapsed = t2 - t1;
			total += elapsed;
		}
		long averageTime = 0;
		averageTime = total / 5000;
		matrix[i][1] = averageTime;
	}
}

public static void main(String[] args) {

	long[][] matrix1 = null; // ???
	Integer[] randomInts = new Integer[] {}; // ???
    runAlgorithm(matrix1, Algorithms::algorithmOnInts, randomInts);


    long[][] matrix2 = null; // ???
	Object[] randomObjects = new Object[] {}; // ???
	runAlgorithm(matrix2, Algorithms::algorithmOnObjects, randomObjects);
}

public static class Algorithms {
	public static void algorithmOnInts(Integer i) {}
	public static void algorithmOnObjects(Object o) {}
}

huangapple
  • 本文由 发表于 2020年7月24日 03:07:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63061456.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定