I have a simple nested structure like this. (This is simplified version of real problem. Some of them actually use Hash_Set. )
class A{ AF a_field; B[] bs;}
class B{ BF b_field; C[] cs;}
class C{ CF c_field; D[] ds;}
class D{ DF d_field; }
static A[] as; // loosely speaking, it has about 4000 D
A psuedo-code method "f" may seem to be complex but it is really easy to understand :-
int f(int TYPE){ //TYPE is 0 - 40
int retur=0;
for(int n=0;n<as.length;n++){
. if(some cheap condition of TYPE){ //use only "TYPE" , cheap (1)
. . as[n].a_field.doExpensiveAA(); //expensive OPERATION, use only "a_field" (Ex retur+=a_field)
. }else if(some cheap condition of TYPE){ //use only "TYPE" (2)
. . as[n].a_field.doExpensiveAB(); //expensive OPERATION, use only "a_field" (Ex retur-=a_field)
. } // .... and a lot of them
. for(int m=0;m<bs.length;m++){
. . if(some cheap condition of TYPE){ //use only "TYPE" (3)
. . . as[n].bs[m].b_field.doExpensiveBA(); (*)//use only "b_field"
. . }else if(some cheap condition of TYPE){//use only "TYPE" (4)
. . . as[n].bs[m].b_field.doExpensiveBB(); (/) //use only "b_field"
. . } // .... and a lot of them
. . for( ..cs .. ){...for(...ds...) {...}...}
. }
}
}
(I desperately add . for indentation.)
"f" is called in every time step :-
public static void caller (){
for(int n=0;n<40;n++){ f(n); }
}
Notice that TYPE is only variable used in condition checking, and it is constant for a single "f" call.
Although each condition checking is really cheap but when it is in the innermost loop, it costs a lot CPU cycles. How to optimize "caller" and/or "f"?
My idea is to
unwrap "f" for each "TYPE" , but there will be a lot of dirty code that is hard to maintain ... like this...
public static void caller (){ f1();f2();f3(); ... f40(); }
Use switch case! It is faster than if-else but switch-case 0 to 40 is ugly. The condition cannot be group like "checking odd/even of TYPE" anymore, so lower maintainability of code.
Edit 1 (answer Partha Sarathi Ghosh): the checking bit is just example ,so index of bit is unimportant, it can be even "> 8", just to note that all condition is depend on TYPE.
Edit 2 : The + - * / is just example, it is an arbitrary function that use the a_field,b_field, etc. (Real case is setting 3D texture or system variables in Opengl) ...cs... and ...ds... is the similar but different expensive operation.
Edit 3 : add information that "A[] as" contains about 4000 D
Edit 4 (answer Partha Sarathi Ghosh): Edit xxx_field's type from int to show they are different Class.