Optimizing your program

The following is just a reply I made on arduino forum. May be useful to share.

This is related to another post a while ago:


In case you run into memory issues,

On the efficiency in function calling:
1) Stack space is filled with return address and parameters you pass to your function
2) Your function may need to establish new variables costing more memory
3) You can rewrite the layers of functions, combining some layers if the lower layer doesn’t ever get called by more than one caller.
4) Using global variables reduces parameters passing to your functions, but also reduces readability re-usability of your code. Say you have one global msg[] for printing messages for serial or lcd and any function uses that array.

Other issues:
1) do you have too many strings? (move them to FLASH with PROGMEM)
2) have you defined too many variables that don’t ever change? (replace with #define)
3) are you using float where a short will suffice? (reduce footprint of each variable)
4) are you using too many overloaded versions of one library function? Say you use lcd.print(int) and lcd.print(string) and lcd.print(float). Try use only lcd.print(string) and use sprintf for formatting since it’s the best way ever.

One Response to Optimizing your program

  1. Pingback: Organize your code – big ASCII art font « Liudr's Blog

Leave a Reply