RE: PicoBlaze Simulator in JavaScript
August 16, 2022 at 5:39 pm
(This post was last modified: August 16, 2022 at 6:14 pm by bennyboy.)
(August 16, 2022 at 12:38 pm)FlatAssembler Wrote: I have always thought switch-case and large else-if statements compiled into the same assembly code.That doesn't make sense. If you have a series of say 100 if/else statements, it is going to have to evaluate those one-by-one as part of the control flow of the app. But a switch statement can be evaluated as a single flow item. Look at your code-- how will you ever arrive at those items near the end of your long if/else chains? There's no way but for the CPU to go through all of those many evalutions one-by-one.
Now, I'm not an expert in computer languages. I can imagine that a very clever compiler will realize that all your conditional statements are being performed on the same object, and just generate a lookup table. But I'd be pretty surprised if that were the case with JavaScript running in a browser.
And EVEN IF they compiled the same (which I really do doubt), the appearance of code matters, too. A switch is easier to read.