简体   繁体   English

如何避免在 Java 中多次调用一个函数?

[英]How to avoid calling a function multiple times in Java?

I'm calling a function with different values to get a particular result.我正在调用具有不同值的函数以获得特定结果。 Is there any way so that I can reduce calling functions like this to improve performance and reliability in Java?有什么方法可以减少这样的调用函数以提高 Java 的性能和可靠性?

func(1,6);
func(4,0);
func(2,7);
func(5,0);
func(3,0);


private static void func(int printer,int printing){
        if(printing == 0){
            for(int j=1;j<=7;j++){
                if(j!=printer){
                    arrayOdd.add(j);
                }
            }
        }else{
            for(int j=1;j<=7;j++){
                if(j!=printer && j!=printing)
                    arrayOdd.add(j);
            }
        }
    }

My first note is that you can replace your function with我的第一个注意事项是您可以将您的功能替换为

private static void func(int printer,int printing){
    for(int j=1;j<=7;j++){
        if(j!=printer && j!=printing)
            arrayOdd.add(j);
    }
}

and it will behave the same.它的行为是一样的。

My second note is that calling a function repeatedly will not have any effect on performance or reliability unless you are using recursion, in which case you may get the classic stack overflow error.我的第二个注意事项是,除非您使用递归,否则重复调用函数不会对性能或可靠性产生任何影响,在这种情况下,您可能会遇到经典的堆栈溢出错误。 Why do you want/need to call the function fewer times?为什么您想要/需要更少地调用该函数?

If the above function can be idempotent( same inputs always produce the exact same outputs) i would suggest memoization ( https://java2blog.com/memoization-example-java/ ).如果上述函数可以是幂等的(相同的输入总是产生完全相同的输出),我会建议记忆化( https://java2blog.com/memoization-example-java/ )。
On seeing the function outputs, it looks like you are just removing the passed values from a fixed array of 7 digits.在查看函数输出时,您似乎只是从 7 位数字的固定数组中删除传递的值。 If thats all you are looking for, just remove the element from the array and memoize the function .如果这就是您要查找的全部内容,只需从数组中删除元素并记住函数即可。 That way the function will execute only 64 times worst case (64 different combinations for 2 digit combinations for 0,1,2 3,4 ,5,6,7 )这样,该函数将仅执行 64 次最坏情况(0,1,2 3,4 ,5,6,7 2 位组合的 64 种不同组合)

you can put the printer's values and printing's values in List or array ,but you need to put the parameters manually, as long as there is no logic hold the inputs.您可以将打印机的值和打印的值放在列表或数组中,但您需要手动放置参数,只要没有逻辑保持输入即可。 I mean some thing like that我的意思是这样的

int final static THE_TIMES_YOU_CALLED_THE_FUNC=5;

List<int[]> list =new ArrayList<>(THE_TIMES_YOU_CALLED_THE_FUNC);
        list.add(new int []{1,6});
        list.add(new int []{4,0});
        list.add(new int []{2,7});
        list.add(new int []{5,0});
        list.add(new int []{3,0});

        for(int i=0;i<list.size();i++){
            funk(list.get(i)[0],list.get(i)[]);
        }
    }

    private static void func(int printer,int printing){
        if(printing == 0){
            for(int j=1;j<=7;j++){
                if(j!=printer){
                    arrayOdd.add(j);
                }
            }
        }else{
            for(int j=1;j<=7;j++){
                if(j!=printer && j!=printing)
                    arrayOdd.add(j);
            }
        }
    }

in the end the compiler will call the func 5 times, and I don't see an advantege of my soultion, but you want in the end to write the func one time.最后编译器会调用func 5次,我没有看到我的soultion的优势,但你最终想写一次func。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM