Which method is faster or more efficient...
Don M
Posts: 1,653
repeat if a do a elseif b do b elseif c do c elseif d do d else do e
or...
repeat case x 1: a 2: b 3: c 4: d 5: e
Comments
x++ was faster by 1ms for every 250 repetitions.
I used a custom clock method to measure the time between the start and completion of the repeat loop. I tested 12 intervals from 500 to 1024000 repetitions. To obtain the next repetition I doubled the last. The data was consistent across each number of repititions.
I hadn't thought to test this yet. But it seems like a good idea, so I went ahead and did it. For this, I only tested 500 and 1024000 repetitions.
I used the following code because it seemed to be the least biased toward any particular version of the if statements.
vs
At 500 repetitions, the case method was 1 ms faster.
At 1024000 repetitions, the case method was 3523 ms faster (1ms per 290.66 repetitions).
So it looks like, in this code, case is 1 ms faster per 250-300 repetitions. Neat!
I suppose it might vary a bit depending on what the conditions are, but its always fun to make data.
Edit:
Oh yeah, clock is at
_clkmode = xtal1 + pll16x
_xinfreq =5_000_000
That's funny. I was wondering the same thing myself and which was a better way of doing. In a project I'm working/learning on I used if statements first and then found I could do the same thing with case. Using case seemed more simple to me.