This is the mail archive of the
guile@sourceware.cygnus.com
mailing list for the Guile project.
Re: So far, so good.
Miroslav Silovic <silovic@zesoi.fer.hr> writes:
> No, this assumption is completely false. switch and if are meant to be
> compiled into different machine-code constructs: if generates a series
> of comparisons, while switch generates a jump-table. I haven't heard
> of any compiler that is able to transform a bunch of ifs into a
> jump-table.
Yes, I was mistaken. I'm pretty sure gcc can convert from switch to
if statements (e.g., if the two values sought in case statements are 3
and 0xdeadbeef, a table would be suboptimal), but I doubt it does the
reverse.
> As you can see, the results are based on different principles. Worse,
> switch results in the code that performs in a constant time in number
> of tests, while ifs perform in linear time.
That's a issue with how the code was constructed. It could be done in
logarithmic time, at some cost in complexity. Also, I'd use
if-then-else, not a sequence of if statements; if the compiler can't
prove that the "then" part of one statement doesn't alter the
condition, it still has to evaluate the other "if" clauses.