No announcement yet.

Very simple optimisation trick.

  • Filter
  • Time
  • Show
Clear All
new posts

  • Very simple optimisation trick.

    One of the things i have at the moment is sorting algorithms more
    or less coming out of my ears and I have learnt something that is
    very simple to make them generally go faster.

    PowerBASIC has default optimisation if you don't turn it off with
    #REGISTER NONE which automatically allocates any available registers
    to variables. What you can do is set the order of priority with
    the LOCAL variables at the beginning of the SUB/FUNCTION so that
    the most used variables are the ones that are allocated to the
    available registers and almost exclusively you will get a measurable
    sped gain from doing so.

    In most of the sorting algorithms there are a pair of variables
    that are used as the indexes for the array locations. These variables
    are the most used ones and are used in recursive loops so any gain you
    can get with the most used variables will make your code go faster without
    having to touch the code at all.

    If the variable are written similar to the following,

    LOCAL counter as LONG
    LOCAL Temp as LONG
    LOCAL Upper as LONG
    LOCAL Lower as LONG
    LOCAL Index1 as LONG
    LOCAL Index2 as LONG

    If Index1 and Index2 are very intensively used in the algorithm,
    just by moving them to the top will get a reasonable speed gain
    without having to modify the code at all.


    [email protected]

    hutch at movsd dot com
    The MASM Forum - SLL Modules and PB Libraries

  • #2
    Or use #REGISTER NONE <U>within</U> the Sub/Function, and then use explicit REGISTER statements to specify which variables should be register variables. For example:
    SUB MySortSub(params)
      LOCAL counter as LONG
      LOCAL Temp as LONG
      LOCAL Upper as LONG
      LOCAL Lower as LONG
      REGISTER Index1 as LONG
      REGISTER Index2 as LONG

    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    mailto:[email protected]