Efficient Programming Techniques

I was looking after some advise on how to be very efficient with programming on the arduino platform.

Any tips to reduce the amount of processing power used during the main loop would be great!

Is using Serial output for debugging very processor intensive? Should I remove all serial output for projects which I deploy in the field?

Any tips to reduce the amount of processing power used during the main loop would be great!

You can't.
The processor runs flat out, all of the time it is active.

Is using Serial output for debugging very processor intensive?

No, not at all. Maybe very memory intensive.

Should I remove all serial output for projects which I deploy in the field?

Probably.

Eschew the use of delay()

Does the project work correctly? Will modifying the code make it work more correctly?

If it does not work correctly something needs to be fixed.

...R

You can considering aggressively putting the processor to sleep (Arduino Playground - HomePage) when you are not using it for a period of time. You would set an interrupt to wake it back up, but I think that generally is going to have to be user input. Note, I haven't yet done this myself but am considering it soon.

Optimization should only be applied if

  1. the program does not meet its performance requirements e.g. number of samples processed per second
  2. the program is running out of resources e.g. RAM / FLASH memory
    and
  3. if you just like optimizing tricks.

Generic tips:

  • use the smallest data format possible - use 8 bit where possible
  • do not use floats if not needed, they have only 6/7 digits accuracy and add quite a bit of memory . A 32 bit long is often faster and offers 9/10 digits. you might have to adjust some math
  • divisions and % are expensive, minimize them
  • merge math expressions
  • use local variables where possible
  • use functions to minimize repeating code (there is a call overhead penalty)

Furthermore

  • redo the design of your sketch, further analysis can result in other algorithms that are faster and smaller and that still meet the requirements.
  • use of libraries can help
  • use other sensors that need less code (price can be accuracy)
  • use another board with more resources (clock/RAM/Flash etc)

Finally, "write unreadable code"