2025-09-26 9:51 AM - last edited on 2025-09-26 10:12 AM by Andrew Neil
I’d like to understand the practical trade-offs — for example, how each level influences execution speed, flash usage, and RAM consumption. Also, are there any best practices or common pitfalls when choosing an optimization level for STM32 projects?
2025-09-26 10:12 AM
This is not really specific to STM32 - there are plenty of general references on this.
https://en.wikipedia.org/wiki/Optimizing_compiler
It will, of course, depend on the particular compiler.
For GCC, see:
https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
You will see that some options target code size, and others target execution speed.
A big problem with optimisation is that it makes debugging harder - the higher the optimisation, the harder it gets to debug.
Probably the biggest pitfall is that optimisation is likely to show up flaws in your code - you'll find loads of forum posts on the lines of "my code was working fine, then I turned on optimisation, and it wouldn't work any more".
A common example is where you haven't put 'volatile' on variables which should have it. You may get away with that with no optimisation, but not with optimisation.