cancel
Showing results for 
Search instead for 
Did you mean: 

Optimization for the speed of the printf function.

Alex Golubev
Associate II

Hi.

Faced the problem of prinyf () function execution speed.

Wrote a simple speed test

HAL_GPIO_TogglePin (GPIOE, GPIO_PIN_8);

uint8_t text [256] = {0};

for (uint32_t i = 0; i <256; i ++)

{

sprintf (text, "% - 5.2f", (i + 10.0 / 2.0));

}

HAL_GPIO_TogglePin (GPIOE, GPIO_PIN_8);

I measure with an oscilloscope the time between setting the 8th leg of port E to a logical unit and reset.

Time is obtained with 1.1 ms cache enabled and 4.4 ms with cache disabled.

I am using the stm32h743 microcontroller. Programming environment stm32cubeide.

Included printf in stm32cubeide as follows settings ---> MCU ---> Miscellaneous ---> -u_printf_float.

5 REPLIES 5
Pavel A.
Evangelist III

So your test shows that it runs faster with cache enabled. Anything new here?

--pa

Alex Golubev
Associate II

I need a runtime to do 256 μS to execute the cache enabled code.

HAL_GPIO_TogglePin (GPIOE, GPIO_PIN_8);
 
uint8_t text [256] = {0};
 
for (uint32_t i = 0; i <256; i ++)
 
{
 
sprintf (text, "% - 5.2f", (i + 10.0 / 2.0));
 
}
 
HAL_GPIO_TogglePin (GPIOE, GPIO_PIN_8);

> I need

Then don't use the library printf(), write your own

JW

Piranha
Chief II

The printf() functions are for... guess it - printing! Printing means converting data to a human readable format. This rises a question of why do you need a 4+ kHz capable printf() at all. If that is for sending data, then drop that ineffective idea and send a binary data with some same format/protocol.

Fixed format, typically convert it to a scaled integer, and use a fast assembler routine to do the string conversion.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..