"Switch cases" and "Else ifs" in loops

Hi.

I have a program where I have a for loop that uses a “switch case” within it, with a few cases to run different code. Also, I use “ifs” and “else ifs”. But, my code seems to run more than ten times slower with this within the loop, rather than outside the loop. So, I tried a “switch” with only one case to see how it handled, and it took about the same about of time as 20 or so cases, and this seemed odd to me. Also, it runs normal with an “if” statement, but whenever I add any “else ifs” within the loop, it slows down significantly.

Is this because I am running a macro within the root interpereter, rather than compiling with the root libraries, or is this a general problem? I figured that “switch cases” and “else ifs” would not slow down the loop much at all since the execution would simply jump to another location, and it wouldn’t have to do any additional calculation. Or, should I just keep my code how I have it now where I do the “switches” and “ifs” BEFORE the loop, and then loop?

Thank you for the help.

Corey

Difficult to come with a dignostic without having one line of code. Anyhow a switch/case is always faster than a suite if if/elseif/else blocks.

Rene

Hi.

[quote=“cfred”]Hi.
Is this because I am running a macro within the root interpereter, rather than compiling with the root libraries, or is this a general problem? I figured that “switch cases” and “else ifs” would not slow down the loop much at all since the execution would simply jump to another location, and it wouldn’t have to do any additional calculation. Or, should I just keep my code how I have it now where I do the “switches” and “ifs” BEFORE the loop, and then loop?

Thank you for the help.
Corey[/quote]

I think, it can be limitation of CINT. It’s not a problem for C++ compiler.

The current CINT byte code optimizer has limit on how many if and loops it can optimize (when the optimization fails, it revert to the entire function to be non-optimized).

Cheers,
Philippe.