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?