6 years, 6 months ago.

Serial Write vs putc while loop, which has less overhead?

I am doing a robotics project which has limited computational power.. The RX/TX system has thus been simplified to bare movement of byte arrays, or chars which are converted from and into datatypes.

My question is, what is the most effective way of moving arrays of chars/uint8_t pure bytes.

putc has a lot of overhead, because for each byte there is a function call to putc, In a 64 element array that is 64(sixty four) function calls per one datapacket. And there's about 1 datapacket every 10ms, which makes 100*64=6400 putc function calls every second.

serial write however only has one function call per buffer array, or does it? I have read in another thread: https://os.mbed.com/questions/6749/Why-Serial-port-write-method-is-protecte/ that all serial write is is a while loop with putc in it, this as far as c++ is not true for write in most cases, is this how your serial library is made?

-Gabriel

1 Answer

6 years, 6 months ago.

Yes, write just wraps around putc, see here.

But even if you optimize that away, underneath it still uses the HAL of your board to send data byte by byte. Which is logical, because that's how UART works. See f.e. implementation for STM32F1 here.

If you have issues with sending data over UART, better up the speed of the UART bus than trying to optimize away function calls. The compiler is probably better at that than you anyway.