User Tools

Site Tools


ugbasic:optimizations
Translations of this page:


OPTIMIZATIONS FOR ugBASIC

This page was created to collect, over time, all the suggestions that have been proposed on github and on the Facebook group. Therefore, they constitute an interesting and practical guide on how to optimize space or time by programming with ugBASIC.

Initialization with constants

In ugBASIC all assignments is implemented by an “assignment per copy”: the value on the right, direct or calculated, is copied to the left “container”. This is also true for the initial values of variables, which means that (unwittingly) you are creating temporary variables to initialize actual variables.

Take this example:

   mapH = 9

It will be translated as (in pseudo assembly):

   MOVE 9 TO temp
   MOVE temp TO mapH

To optimize this point it is sufficient to use the hash character (#), to be prefixed to the value you want to be considered a constant. In this way, the value will NOT be implicitly converted to a temporary variable but used AS IS.

Again:

   mapH = # 9

It will be translated as (in pseudo assembly):

   MOVE 9 TO mapH

Using constants

The ugBASIC language allows you to define constants, to be used in place of fixed values or expressions that will be evaluated once and for all. The advantage of using constants is that it is the compiler, not the retrocomputer, that does the calculations. Very useful!

Take this code:

  north=1: x = north

This code will be translated into (pseudo assembly):

  MOVE 1 TO temp
  MOVE temp TO north
  MOVE north TO x

Instead, if you define it as a constant:

  CONST north=1: x = #north

This code will be translated into (pseudo assembly):

  MOVE 1 TO x

Using vectors

In general, accessing two-dimensional or multidimensional arrays is quite slow under ugBASIC, because it requires to calculate the precise offset where the element the program wants to access is located. It is a complex calculation, with many additions and multiplications, and it takes a long time. To avoid it, then, it is better to use vectors (one-dimensional arrays) which guarantee, in case we have less than 256 elements, quick access. Alternatively, reducing the number of sizes helps decrease the size of the overall executable.

Take this code:

  DIM a(3,4)
  a(1,1) = 42
  a(2,2) = 84

It could be better to rewrite it as:

  DIM a0(4), a1(4), a2(4)
  a1(1) = 42
  a2(2) = 84

Prefer prefix form

For all arithmetic operations where the target of the operation is one of the operands, ugBASIC supports both the infixed form (x = x + y) and the prefixed form (ADD x, y). However, the prefixed form is significantly faster because it is translated without temporary values and, often, with the corresponding opcode assembly.

Take this code:

  x = x + y
  

it will be translated in (pseudo assembly):

  MOV x TO temp
  ADD y TO temp
  MOV temp TO x

Instead, this code:

  ADD x, y

it will be translated in (pseudo assembly):

  ADD y TO x

This is the table of each operator (infix and prefix forms):

operation prefix infix
sum ADD +
difference SUB -
division DIV /
multiplication MUL *