the task is to optimize the two functions

the first of them needs to pass the operation number (integer) and two operands (real numbers) and then with the help of the select operator with operands the corresponding action was performed

if (OperationNumber! = 0) { switch (OperationNumber) { case 1: OperationPlus (); break; case 2: OperationMinus (); break; case 3: OperationMultiplication (); break; case 4: OperationDivision (); break; case 5: OperationDiv (); break; case 6: OperationMod (); break; } 

The essence of optimization is to replace the select statement with an array whose elements are mathematical operations, that is, instead of choosing a number, the algorithm provides for simply calling the array element with the transferred number.

the second function is almost the same, only one number is transferred to it, to calculate a certain function (such as Sin, Cos, Tg).

If in Java it is possible to organize the above-described optimization, then please tell me how to create such an array, and how to call operations / functions from it.

If anyone has free time, I would be very grateful for the help, as I just started learning Java.

    4 answers 4

     // Π·Π°Π΄Π°Π΅ΠΌ интСрфСйс абстрактной ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ // Π³Π΄Π΅ E Π·Π°Π΄Π°Π΅Ρ‚ Ρ‚ΠΈΠΏ ΠΎΠ±Ρ€Π°Π±Π°Ρ‚Ρ‹Π²Π°Π΅ΠΌΡ‹Ρ… ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ² interface IFunction<E> { public E calculate(E x, E, y); // ΠΈΠ»ΠΈ (E ...args) } Ρ€Π΅Π°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ интСрфСйсов: class AddFunction implements IFunction<Integer> { public Integer calculate(Integer x, Integer y) { return Integer.valueOf (x.intValue() + y.intValue()) } } class MulFunction implements IFunction<Integer> { public Integer calculate(Integer x, Integer y) { return Integer.valueOf (x.intValue() * y.intValue()) } } // создаСм Ρ‚Π°Π±Π»ΠΈΡ†Ρƒ с ΠΌΠ°ΠΏΠΏΠΈΠ½Π³Π°ΠΌΠΈ Map<Integer,IFunction<Integer>> map = new HashMap<Integer,IFunction<Integer>>(); ... map.put(Integer.valueof(0), new AddFunction()); map.put(Integer.valueof(1), new MulFunction()); // использованиС: public Integer test(int opcode, Integer x, Integer y) { return map.get(Integer.valueOf(opcode)).calculate(x, y); } 

    same but using an array:

     // маппинг с использованиСм массива: IFunction<Integer> array = new IFunction<Integer>[2]; ... array[0] = new AddFunction(); array[1] = new MulFunction(); // использованиС: public Integer test(int opcode, Integer x, Integer y) { return array[opcode].calculate(x, y); } 
    • maybe someone knows how to organize this algorithm using a static array, since here is a search on the map, but it is not needed because the index is known - pj-infest
    • the essence of this does not change much, if we take that the opcode is an index in the array, then everything is very simple in general. Now let's fix an example - jmu
    • I have one more solution, here the interface is used and for each operation a separate class, with identical methods. The classes are pushed into the array, and then just call the corresponding element of the array and execute its method (which is described in the interface). The example is identical to yours, I have a corrected version. thus bypasses the cases suggested by me, and the search on the map is avoided. What I need, thanks for the help! - pj-infest
    • in a small application, it doesn’t matter what you are using an array or map, performance is not so critical. In such situations, it is more convenient to use a map than just an array. After all, the key can be any type besides the banal number / string, carry other useful data. eg: instead of int in this example, you can use enum ... - jmu
    • and if during the development of a large project programmers will perform these almost negligible optimizations, then as a result will the work of a large software product be optimized in a significant way? or from such small optimizations, even for a large project there will be no sense? - pj-infest

    My problem has been solved, an array of pointers can be used in C ++, and an array from a class object needs to be vicorist-only if the search for a function is called many times a second. since the function identification calls will not be executed as often, I use the case as the members of the forum advised me.

    Very grateful for the help of @jmu and @mikillskegg

      You can try to make an array of objects, each of which contains one method that takes 2 parameters and calls the desired function, passing these parameters to it. Only I doubt whether it will give a prize.

      • Why are you in doubt, can't I avoid a certain number of checks and save CPU time? My problem is that I do not understand the syntax of such a construction, if you can, please give an example. - pj-infest

      I'm not sure that the creation of a heap of additional functor classes here is generally an optimization. Is it possible to solve the problem in a more suitable functional or scripting language?

      • 2
        in C / C ++, this task would be solved elementarily with the help of an array of pointers to functions. - skegg
      • completely agree, about the good old C, everyone sometimes forgets - Mage
      • The task cannot be solved in languages ​​other than JAVA, this is a learning task. But does JAVA have the ability to create an array of function pointers? - pj-infest
      • Well, this possibility is hypothetically there, you need to enter the Java Reflection API, but this is not at all the same as function pointers. In general, I want to look into the eyes of the teacher who sets such tasks. He is a bad person. - Mage
      • Well, if the teacher asked, there is nowhere to go, you have to do ... - skegg