Which is faster in Spin- case statement or if statement?
Don M
Posts: 1,652
in Propeller 1
Title says it all...
Comments
No idea. But easy enough to test. Just read the cnt value, do a number of loops and read cnt again and subtract the first cnt.
In general I'd recommend writing the code to be clear for yourself and your readers. Use whatever construct (case or if) is more natural -- if you just have 2 choices, if/else is natural, if you have a lot more then case is probably more natural. If you're getting to the point where counting cycles to see whether case or if is faster, then you probably want to re-write that part of your code in PASM (or use flexspin to do that for you).
IIRC case is faster than if for more than two choices.
The great thing about using the Propeller is that you don't have to guess. I frequently do timing tests on bits of code like this:
Yes, a great strength of the Propeller and spin. Even quite complex code like sorting a list of strings will (given the same input) always run at the same speed, down to the cycle.
What is the reason for subtracting 368?
368 would be the number of clocks taken to so this test with nothing else (ie no if/case/etc) performed) within.
[deleted]
If you run that code as-is (hint, hint) the result will be 0, which is what we expect with no code between the checkpoints. As Ray pointed out, capturing of the timer and processing the result takes 368 system ticks (in this configuration); with that removed from the result, your tests will be accurate.
Wasn't there some form of jump-table being considered? I seem to remember Chip mentioning it, some time ago.
Not in the P1. In Spin2 there is a CASE_FAST structure that uses an internal jump table.
Thanks everyone.