简体   繁体   中英

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?

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/ ).
On seeing the function outputs, it looks like you are just removing the passed values from a fixed array of 7 digits. 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 )

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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