Previously, I tested x:=x+1 vs x++
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.
x:=0
repeat r
if x==0
x++
elseif x==1
x++
elseif x==2
x++
elseif x==3
x++
else
x:=0
vs
x:=0
repeat r
case x
0:x++
1:x++
2:x++
3:x++
other:x:=0
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
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
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.
BST has an option to list the compiled byte codes. You could make up a program with both statements, compile it, and look at the generated code. That still doesn't give you an execution time. For that, you'd have to look at the Spin interpreter itself and figure out the execution time for the specific interpretive instructions involved.
As Bits points out, you can use the Propeller to time itself -- I do this all the time when wondering which code fragment is faster than another. I do this so frequently that I made a template program for the process.
'Case' is certainly cleaner and more compact if not faster. The 'Case' snippet you posted is seven lines as opposed to the eleven lines in the 'If' section.
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.