Page 1 of 1

Does code on Arduino IDE run faster than CubeIDE?

Posted: Mon Oct 25, 2021 12:28 am
by vlad
Hi there

I was developing a project on Arduino using a generic STM32H743VIT board. Due some drawbacks of the Arduino library, mainly concerning to ADC and time interrupts, I decided to change to STM32CubeIDE, that supposedly should be more efficient in utilizing the processor resources. After successfully make the transition to new code, the first tests were, let’s say, weird. Data processing was terrible slow. So I decided to make additional tests by measuring the computation time of sum, product, division and also some functions (cosine and exponential), and compare them with the same code running on Arduino IDE. The code I employed to measure the processor performance in CubeIDE and Arduino IDE were:

On STM32CubeIDE:

Code: Select all

 
{float fl_2, fl_3;
uint32_t itop = 1000000;
fl_2    = 1;
fl_3    = 0;
to = HAL_GetTick();
for (i = 0; i < itop; i++)
{
	fl_2    = fl_2 + fl_3;
	fl_3    = fl_2 + fl_3;
}
tf  = HAL_GetTick() - to;
CDC_print_cp("Float sum (ms): ");
CDC_print_int((tf - tloop)/2);
CDC_print_ln();}
On Arduino:

Code: Select all

 
{float fl_2, fl_3;
uint32_t itop = 1000000;
fl_2    = 1;
fl_3    = 0;
to = micros();
for (i = 0; i < itop; i++)
{
	fl_2    = fl_2 + fl_3;
	fl_3    = fl_2 + fl_3;
}
tf  = micros() - to;
Serial.print("Float sum (ms): ");
Serial.print((tf - tloop)/2000);
Serial.println();
Serial.println();}
To my surprise, Arduino was 100 times faster than CubeIDE. The measured times were: 3646 micro seconds, or 3.646 milliseconds on Arduino IDE and 79 milliseconds on CubeIDE. The empty loop elapsed time was subtracted from these values. Since my knowledge of CubeIDE configuration is far limited, I can only suppose that there are some wrong parameters, like clock or compilation options. Unfortunately I couldn't find any explanation on net over this problem, and I really believe that CubeIDE is at least as fast as Arduino. Does anyone can say what I’m doing wrong?

Re: Does code on Arduino IDE run faster than CubeIDE?

Posted: Mon Oct 25, 2021 2:14 am
by ag123
stm32duino(s) turns on the FPU, that's probably why

Re: Does code on Arduino IDE run faster than CubeIDE?

Posted: Mon Oct 25, 2021 2:35 pm
by vlad
ag123 wrote: Mon Oct 25, 2021 2:14 am stm32duino(s) turns on the FPU, that's probably why
And that means that CubeIDE doesn't turn it on by default?

Re: Does code on Arduino IDE run faster than CubeIDE?

Posted: Mon Oct 25, 2021 11:44 pm
by djix123
Looks like the CubeIDE has an option setting to turn on the FPU in:

Project properties -> C/C++ Build -> Settings -> Tool Settings -> MCU Settings

per: https://community.st.com/s/question/0D5 ... m32cubeide

I've not tried it myself (use VSC / PlatformIO).

[As an aside, its worth noting that 'Makefile' projects generated by CubeMX with have the compiler/linker options set to enable the FPU but using the the stm32pio scripts to convert a CubeMX project to PlatformIO requires manually adding the compiler/linker flags, in case one decides to move away from the CubeIDE]

Re: Does code on Arduino IDE run faster than CubeIDE?

Posted: Tue Oct 26, 2021 6:13 pm
by vlad
djix123 wrote: Mon Oct 25, 2021 11:44 pm Looks like the CubeIDE has an option setting to turn on the FPU in:

Project properties -> C/C++ Build -> Settings -> Tool Settings -> MCU Settings

per: https://community.st.com/s/question/0D5 ... m32cubeide

I've not tried it myself (use VSC / PlatformIO).

[As an aside, its worth noting that 'Makefile' projects generated by CubeMX with have the compiler/linker options set to enable the FPU but using the the stm32pio scripts to convert a CubeMX project to PlatformIO requires manually adding the compiler/linker flags, in case one decides to move away from the CubeIDE]
Yes, I just have noticed that. And in my project the FPU was enabled by default. Moreover, the benchmark I've posted here was just a small piece of total benchmark. I measured times for uint32_t, int32_t, uint16_t, int16_t and float. The processing times were measured for sum, difference, product and division. All measured times were greater in STM32CubeIDE, from 20 to 100 times slower. I still don't have any idea of what is happening. Do you have any other guess?

Re: Does code on Arduino IDE run faster than CubeIDE?

Posted: Wed Oct 27, 2021 1:07 am
by djix123
My best guess would be clock related. In the Arduino setup the MCU clock source is the PLL. Looking at the parameters in ‘generic_clock.c’ I would guess that the frequency is toward the max end of the range.

Not sure what the CubeIDE does, but CubeMX projects default to the MCU clock source being the HSI which is almost certainly slower than the Arduino / PLL configuration.

In your project there should be a function called something like: SystemClock_Config that sets the clock parameters. In CubeMX projects this is in the main.c file. This will tell you how the clock is being set, and any differences to the Arduino set up could explain the performance differential,

Re: Does code on Arduino IDE run faster than CubeIDE?

Posted: Wed Oct 27, 2021 1:25 pm
by fpiSTM
@ag123 and @djix123 , (well 123 team :D ) are right.

- Check FPU settings
- System clock config
- maybe also the gcc optimization option (By default Arduino defines -Os for smallest)

By default Arduino uses the nano libc while I think Cube uses the standard one.

apart from that I don't see any other difference.

Re: Does code on Arduino IDE run faster than CubeIDE?

Posted: Wed Oct 27, 2021 1:37 pm
by djix123
Also worth checking if the ICache and DCache are enabled in the CubeIDE project - in the default CubeMX project they are are set to disabled.

Re: Does code on Arduino IDE run faster than CubeIDE?

Posted: Wed Oct 27, 2021 2:44 pm
by ag123
FPUs are a feature of stm32f4xx and above, on top of them being fast mcus, previously, there's even 'dhrystone, whetstone' 'benchmark competitions'
viewtopic.php?f=7&t=27
try running them on your stm32f7 mcus for a comparison. I think it is likely plenty fast, given that stm32f7 are superscalar cpus

I think they are there in the examples as well
https://github.com/stm32duino/STM32Exam ... nchmarking

Re: Does code on Arduino IDE run faster than CubeIDE?

Posted: Wed Oct 27, 2021 3:05 pm
by vlad
SOLVED!!! Thank you @djix123. It wasn't clear on documentation that I should use PLL clock instead of HSI on MCU. After some adjustments on MX, processing speed is now close to what I should expected. I'll try also the suggestions from @fpiSTM and the cache setup.
Best regards from Brazil.