Menu

make your code faster

2022-04-29
2022-07-02
  • stan cartwright

    stan cartwright - 2022-04-29

    Ideas here for making your code run faster.

    1- goto
    for c1=1 to 100
    if var(c1)=var2 then var3=1
    next

    for c1=1 to 100
    if var(c1)=var2 then goto endfornext
    next
    endfornext: var3=1

    2-don't use and
    if var1=5 and var2=20 then

    if var1=5 then if var2=20 then

    3-for next vs repeat
    for ptr=1 to 100
    next

    ptr=1
    repeat 100
    ptr++
    end repeat

    4-thinking... more goto stuff... any ideas to add?

     

    Last edit: stan cartwright 2022-04-29
  • Anobium

    Anobium - 2022-04-30
    1. Use ROTATE rather than Divide or Multiply. If the sum is 2,4,8,16,32,64 etc... use ROTATE many times.
    2. Use multiple for-next using Bytes rather than one for-next using a Word or Long loop variable.
    3. Use a constant to set bits rather than a variable. PORTx.0 thru to PORTx.7 with a case statement uses more code but will be a lot faster than PORTx.variable
     
    • stan cartwright

      stan cartwright - 2022-04-30

      Good tips. Cheers.

       
  • William Roth

    William Roth - 2022-07-02

    When swapping nibbles in a byte variable, GCB supports the function "SWAP4". A macro is much faster, especially on a PIC, where it is literally 10 x faster. 1 instruction cycle vs 10 instruction cycles. On an AVR, the call to function and the return are eliminated making it faster as well.
    (AVR section untested) .

    Macro SwapNibbles(IN Swap_IN, OUT Swap_Out)
       #ifdef PIC
           SWAPF Swap_IN, W
           MOVWF Swap_OUT
       #endif
    
       #ifdef AVR
           'Dim Swap_IN   ;Alias SysCalcTempA
           'Dim Swap_OUT  ; Alias SysCalcTempX
           mov Swap_OUT, Swap_In
           asm swap Swap_OUT
       #endif
    End MAcro
    
     
  • Chris Roper

    Chris Roper - 2022-07-02

    Nice, certainly couldn't be optimized any more than that.

     

Log in to post a comment.