0

The following code block is repetitive, but I'm not too annoyed by it:

    // method 1
    for (int i = 0; i < RUNS; i++) {
        otherObject.method1();
    }

    // method 2
    for (int i = 0; i < RUNS; i++) {
        otherObject.method2();
    }

    // method 3
    for (int i = 0; i < RUNS; i++) {
        otherObject.method3();
    }

    // method 4
    for (int i = 0; i < RUNS; i++) {
        otherObject.method4();
    }

However, if this repeats for let's say 10 method, it would start to bother me that I had this big, repetitive block in the middle of my code. Instinctively (and perhaps naively), I would want to change it to something like this:

    for(int j = 0; j < 4; j++){
        for(int i = 0; i < RUNS; i++){
            switch(j){
            case 0: otherObject.method1(); break;
            case 1: otherObject.method2(); break;
            case 2: otherObject.method3(); break;
            case 3: otherObject.method4(); break;
            }
        }
    }

If the number of methods to call, or the number of RUNS began to get very large, would it become inefficient to keep repeatedly running through the switch statement? If so, is there a good workaround to this problem?

Kieran Moynihan
  • 593
  • 3
  • 15
  • 1
    put the methods in an array and use two loops (no switch) – user85421 Sep 30 '18 at 19:06
  • *Opinion:* Of those two choices, definitely use multiple loops. – Andreas Sep 30 '18 at 19:11
  • Do you ever need to call the methods _not_ in this loop, but still need to know which one corresponded to `j = 0`, `j = 1`, etc.? If no, then the array approach @CarlosHeuberger recommended is good. Otherwise, I'd put them in a `Map` where the key is the corresponding `j` value, and the `Runnable` is the method to invoke (either `otherObject::method*` if on java 8; or the more complex old `new Runnable() { ... }` incantation if on an earlier version). – BeUndead Sep 30 '18 at 19:33
  • @CarlosHeuberger Am I to assume from your response that referencing array indices would be faster than checking a switch statement each iteration (since that's the main point of my question)? – Kieran Moynihan Sep 30 '18 at 19:38
  • https://stackoverflow.com/questions/15621083/why-does-java-switch-on-contiguous-ints-appear-to-run-faster-with-added-cases/15621602#15621602 – piradian Sep 30 '18 at 19:51
  • Is it a requirement that the methods run in that particular order? – MC Emperor Sep 30 '18 at 20:07
  • @MCEmperor No, but it is required that they are run in a specific order (not randomly). – Kieran Moynihan Sep 30 '18 at 20:16
  • If it is allowed that the order is `method1()`, `method2()`, `method3()`, `method4()`, `method1()`, `method2()` et cetera, then you can drop the switch altogether: `for (int i = 0; i < RUNS; i++) { obj.method1(); obj.method2(); obj.method3(); obj.method4(); }` – MC Emperor Sep 30 '18 at 21:13
  • not faster, easier to maintain - if speed is the main point of the question, it should be mentioned there... and then use multiple loops, no switch (despite switch being pretty fast) – user85421 Sep 30 '18 at 21:43

0 Answers0