[Mono-list] [Off-topic] "if else" or "switch" ?

Paolo Molaro lupus@ximian.com
Sun, 17 Oct 2004 16:45:25 +0200


On 10/17/04 Michal Moskal wrote:
> > No, I don't think that it's a completely a runtime issue. There are at least 
> > three fundamentally-different mechanisms for handling a switch statement, 
> > including hashing, binary search, and a linear if/else. With CIL, there is 
> > the further complication of using the switch instruction versus the compare 
> > and branch sequences.
> 
> However, when you have switch in the CIL, it's up to the JIT how to handle
> it. And choosing between the if-else and the jump-table implementation
> can be machine dependent, performance wise.

It's also cpu-dependent: some processors may not do branch prediction
with indirect branches generated by a switch, but they do it with
conditional branches generated with if/else.
We may add the code to turn a switch with less than 3-4 destinations
to a if/else sequence, but I'm not sure if this is really a speed 
problem in practice. Marcus reported behaviour may be due to different IL code 
generated by the compiler: I'd like to see the actual IL code
to determine what actually caused the performance issue he reported.

lupus

-- 
-----------------------------------------------------------------
lupus@debian.org                                     debian/rules
lupus@ximian.com                             Monkeys do it better