Shop OBEX P1 Docs P2 Docs Learn Events
Which method is faster or more efficient... — Parallax Forums

Which method is faster or more efficient...

Don MDon M Posts: 1,653
edited 2012-08-04 11:21 in Propeller 1
 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

  • CircuitsoftCircuitsoft Posts: 1,166
    edited 2012-08-03 14:12
    I would imagine case, since there's only one jump point, but I haven't tested this and don't know for sure.
  • varnonvarnon Posts: 184
    edited 2012-08-03 14:29
    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
  • ihmechihmech Posts: 179
    edited 2012-08-03 16:35
    Don M wrote: »
     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.
  • Mike GreenMike Green Posts: 23,101
    edited 2012-08-03 16:47
    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.
  • BitsBits Posts: 414
    edited 2012-08-03 17:40
    I have found "if/s' are faster than 'case" and you could simply find out by using this.
  • JonnyMacJonnyMac Posts: 9,194
    edited 2012-08-04 09:00
    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.
  • lardomlardom Posts: 1,659
    edited 2012-08-04 09:33
    '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.
Sign In or Register to comment.