Shop OBEX P1 Docs P2 Docs Learn Events
What is faster? — Parallax Forums

What is faster?

Zap-oZap-o Posts: 452
edited 2009-08-25 20:18 in Propeller 1
Is it better / faster to use IF statements or a Case statement? I intend on using many statements and for some reason I want to think that case is faster but not sure.

Comments

  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2009-08-25 19:35
    you could put the two types into a program and check the CNT timer at the beginning and the end and figure out the difference between the two.

    My guess would also be that the CASE command is faster especially when a significant number of conditions are checked. I would also imagine CASE would use less memory than IFs.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    April, 2008: when I discovered the answers to all my micro-computational-botherations!
  • jazzedjazzed Posts: 11,803
    edited 2009-08-25 19:49
    Spin CASE is very clumsy. It is also limited to 64 cases ... you end up mixing IF and CASE then. If you want to see how it behaves, use BSTC or Homespun for listings and a debugger.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve

    Propeller Tools
  • CounterRotatingPropsCounterRotatingProps Posts: 1,132
    edited 2009-08-25 20:02
    jazzed said...
    Spin CASE is very clumsy. It is also limited to 64 cases ... you end up mixing IF and CASE then.
    OH now that's ugly ! freaked.gif·· The case limit doesn't seem to be documented in the PropMan - did you stumble on that with the 65th case?·


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • KyeKye Posts: 2,200
    edited 2009-08-25 20:05
    The case statement also waste a ton of memory. Really the difference is between a few longs when using ifs.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • Bobb FwedBobb Fwed Posts: 1,119
    edited 2009-08-25 20:18
    CASE is faster...here is the test code I used:

    PUB Main | value, start, time, tmp
    
      value := 10
    
      start := cnt
      IF (value == 0)
        tmp~~
      ELSEIF (value == 1)
        tmp~~
      ELSEIF (value == 2)
        tmp~~
      ELSEIF (value == 3)
        tmp~~
      ELSEIF (value == 4)
        tmp~~
      ELSEIF (value == 5)
        tmp~~
      ELSEIF (value == 6)
        tmp~~
      ELSEIF (value == 7)
        tmp~~
      ELSEIF (value == 8)
        tmp~~
      ELSEIF (value == 9)
        tmp~~
      ELSEIF (value == 10)
        tmp~~ 
      time := cnt
    
      DEBUG.dec(time - start - 368)  ' worst case: 8032 best case: 1200
      DEBUG.tx($0D)    
    
      start := cnt
      CASE value
        0: tmp~~
        1: tmp~~
        2: tmp~~
        3: tmp~~
        4: tmp~~
        5: tmp~~
        6: tmp~~
        7: tmp~~
        8: tmp~~
        9: tmp~~
        10: tmp~~
      time := cnt
    
      DEBUG.dec(time - start - 368)  ' worst case: 6224 best case: 1280
      DEBUG.tx($0D)
    
    



    IF/ELSEIF is worst case: 8032 cycles best case: 1200 cycles
    CASE is worst case: 6224 cycles best case: 1280 cycles
    And the difference is 8 longs in memory.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    April, 2008: when I discovered the answers to all my micro-computational-botherations!
Sign In or Register to comment.