Skip to main content
September 26, 2025
Question

How does optimization level affect performance and memory usage ?

  • September 26, 2025
  • 1 reply
  • 418 views

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?

This topic has been closed for replies.

1 reply

Andrew Neil
Super User
September 26, 2025

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.

 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.