Search

Efficient Coding

Some thoughts on writing efficient Flowcode - please share your ideas too.

These tips generally apply to all computer languages - and writing smaller efficient code is an art - and no-one ever writes perfect code.

It's worth pursuing because smaller programs can run on smaller MCUs (cheaper) They are easier to debug and often quicker to write (less lines of code = less typing / mousing). Practice and experience help.


All though it may not make a big difference to a hobbyist - just throw a Raspberry Pi at the problem - for industry it can make a huge difference. Saving a few pence on each unit of the Gizmo5000 (tm) - expected sales 10,000,000+++ makes a difference to our bottom line of $$$$$.


So - some tips for writing efficient Flowcode.


1) Keep it local.


Only make variables global if they need to be global. This should be limited to some state variables. Some variables that are needed to be accessed interrupt handlers (timers/UART input queues) and not much else.

The scope of a variable is the code in which that variable can be seen (and modified etc) - minimising this minimises the risk of unpleasant side effects and also makes code easier to read.

Global variables occupy memory at all times - locals only during the macro execution.

2) Split your program into Macros (functions)


If code is repeated then rather than cutting and pasting it as a block - split it off into a function. The point at which this becomes a good idea depends on the size of the code.

There is some overhead to calling a macro (or function) - in terms of performance (the program needs to jump to a new location - saving some registers and the program counter onto the stack. However - aim for maximum readability and split where appropriate. If the macro is small the compiler might 'inline' it in the code - for speed of execution.


Macros shouldn't be too large - smaller blocks of code are easier to read and simpler to debug.


Macros are also easier to re-use in other programs.


3) Loop counters.


Use a (local) variable as a loop counter - each time you create a 'for' loop in Flowcode it creates a variable to use as loop counter (as a global). These occupy memory at all times.

Use the index as an index - see the comment on arrays below


4) Use Arrays.


Arrays are a very powerful part of Flowcode and other programming languages. They allow data to be grouped together and accessed easily.


They are easily defined - declare variable as name[10] for an array of 10 values (accessed as name[0] .. name[9]


So, rather than: (Note three variables would be easily handled - but imagine having 10 or 100 - things get messy fast)


a = input(1)

b = input(2)

c = input(3)



This is not recommended (from a real example...)


Use

instead




5) Look it up


Sometimes - rather than a 'switch' statement. It is quicker (and smaller) to use a lookup table.

This can be a LUT in Flowcode or an array of data.


6) Calculate that value.


One large block of code I saw had a switch (actually multiple switch statements - Flowcode only allows 10 'cases') statement for each character of the alphabet (in upper and lower case. Also the digits etc)


This effectively did:

if c = 'A' then x = 65 +constant;

if c = 'B' then x = 66 + constant;

if c = 'C' then x = 67 + constant;

etc.


Then same result could be achieved with


if(c >= 'A' and c <= 'Z') then x = c + constant.

This change saved ~4k of program space.


7) Test your code.


!!!!!


8) Your ideas - please - comment and add your top tips..




44 views2 comments

Recent Posts

See All