I have a condition I do not know at compile time but which once initialized does not change during the program. My program looks something like this
void cond_func(const int cond){
switch(cond){...}
}
void loop_func(const Some_class& param){
for(...) cond_func(param.cond);
}
Is branch prediction able to tell that the cond
should always evaluate to the same value? Or is it better to write the switch
before the loop and then call appropriate functions (which would lead to less readable code with multiple code duplication). Or can I do something to give a compiler hint that this condition will stay constant, like extract the value const int cond = param.cond)
before the loop? Would something change if I also pass the const Some_class& param
during the loop to some other functions and hence theoretically change value of cond
through some weird pointers and/or const_cast
(although I`m not)?
My loop is usually millions and more long so I`m not interesting in first few calls to predict the branch but every ms spent in some condition evaluation in every cycle counts....