I know the basic functional differences between printf and sprintf. But, I would like to know about some timing/delay related differences between them both. Apparently, I want to use it in one my tasks for a custom built RTOS. What do you reckon ? I would like to know more how it would affect the performance of the system. (if any). Usually, I dont use print functions because of the massive delay but, I have to mandatorily use it here.
FYI, output is displayed on the terminal window using RS232.
Thanks.
The primary issue here is that
printf()writes tostdoutwhich can (and almost certainly will) block the calling thread. It’s not at all uncommon on an embedded system forstdoutto be a very slow RS232 port.For this reason you never do this in a real-time thread as it rapidly becomes high non-real-time.
Writing into a buffer with
sprintf()is fairly cheap (providing the buffer has already been allocated). It certainly doesn’t block.You’ll probably find that your RTOS provides an asynchronous logging mechanism that can be called from a real-time thread without the risk of blocking. This is will be nothing more than a ring-buffer into which you write your terminal output and a lower priority thread to print it to the terminal.