Skip to main content
Associate
April 7, 2026
Question

GCC -O2 optimization ~3% lower performance than Keil for motor control application

  • April 7, 2026
  • 3 replies
  • 325 views

Subject: Performance difference between GCC (-O2) and Keil ARM Compiler in motor control application

Description:

I am currently developing a motor control application using STM32F303 and comparing the performance of two development environments: STM32CubeIDE (GCC ARM toolchain) and Keil µVision (ARM Compiler).

Hardware & Software Environment:

  • MCU: STM32F303

  • STM32CubeIDE Version:2.1.1

  • Keil MDK Version: v5

  • Motor Control SDK Version: X-CUBE-MCSDK v6.4.2

  • Optimization Level: -O2 for both compilers

  • FOC Algorithm: [hall FOC]

  • PWM Frequency: [16 kHz]

Test Methodology:
I have ported the exact same motor control source code to both environments. Performance is measured by:

  1. I noticed that the .hex file generated by Keil (100 KB) is smaller than the one generated by STM32CubeIDE (103 KB). Does this come from the compiler difference?
  2. Has anyone performed similar benchmarks between GCC and ARM Compiler for motor control and found strategies to close the gap?

3 replies

Peter BENSCH
Technical Moderator
April 7, 2026

Welcome @Xsl_01, to the community!

The small code size difference you observe (about 3%) between the Keil ARM Compiler and GNU GCC / STM32CubeIDE is expected and mainly due to different optimisation strategies in the two toolchains.

Commercial compilers such as the ARM Compiler are typically tuned for slightly better code size and performance in embedded use-cases, which explains their remaining advantage, even though GNU GCC has improved a lot over the last years. In many motor-control projects this gap can be reduced further by carefully adjusting GCC options (e.g. -Os or -O3, LTO with -flto, -fdata-sections/-ffunction-sections plus --gc-sections, and the correct -mcpu/FPU settings).

Whether the commercial compiler is worth it usually depends less on a few kilobytes of code size and more on your overall constraints (timing margin, safety/certification, tooling and support) - not to forget the price you are willing to pay for it.

Does this answer your questions?

Regards
/Peter

 

Xsl_01Author
Associate
April 8, 2026

I'm very sorry, my unclear explanation may have caused a misunderstanding. My question is: using the same MC_SDK project, when generating the program with Keil and Cube IDE respectively, and testing the same motor under otherwise identical conditions, the efficiency measured with the program generated by Keil is 3% higher than that generated by Cube IDE.

Ozone
Principal
April 8, 2026

Not sure how you define "efficiency" in this case.
But for code size or runtime of certain routines, a difference of 3% is really minor.
In other instances and architectures, 20 to 50% difference are not uncommon.

By the way ...

>  -  Optimization Level: -O2 for both compilers

This is not necessarily an exact match.
You would need to investigate the individual optimisation settings both compilers implicitly enable with "-O2".
And you can trim any optimisation towards either code size or speed.

Andrew Neil
Super User
April 7, 2026

@Xsl_01 wrote:
  1. I noticed that the .hex file generated by Keil (100 KB) is smaller than the one generated by STM32CubeIDE (103 KB). Does this come from the compiler difference?

The size of the Hex file is not a great indication of the size of the actual executable code.

There's a lot of overhead in the Hex format, and lot of choices in how to represent the data.

Such a small difference could easily just be down to formatting & overhead.

 


@Xsl_01 wrote:

2. Has anyone performed similar benchmarks between GCC and ARM Compiler for motor control and found strategies to close the gap?


Keil did used to have some - you'd have to ask them about that.

But, as @Peter BENSCH suggested, it really shouldn't come as a surprise to find that a commercial compiler costing over a thousand pounds out-performs a free one!

If you've paid for a Keil licence, you would certainly hope that would be the case!

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.
Andrew Neil
Super User
April 8, 2026

@Xsl_01 wrote:
  • STM32CubeIDE Version:2.1.1

  • Keil MDK Version: v5


Note that the current version of Keil MDK seems to be 6 ...

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.