The standard library function sprintf() is not inefficient, it's just that it can do so many things its an H-bomb-to-kill-an-ant if you just want to print out the contents of a variable. While I haven't done any size comparisons, my guess is that sprintf() would use more code space that just a simple print statement would.
I'm still trying to adapt my coding technique to the tiny amount of RAM available and I'm just a little nervous.
I read quite a few things but I guess the following post from the forum sums up my confusion. http://forum.arduino.cc/index.php/topic,131289.0.html
I didnt' think it was made clear in the post, whether sprintf is safe or not, preferred or supported or not.
MattS-UK:
I read quite a few things but I guess the following post from the forum sums up my confusion. http://forum.arduino.cc/index.php/topic,131289.0.html
I didnt' think it was made clear in the post, whether sprintf is safe or not, preferred or supported or not.
Are you sure that is the right link?
There is nothing in that thread that suggests that sprintf() is poor or costly.
It contains all the print/ln functionality of the serial library ( mostly untested ).
Heres how it works:
//Create a buffer
char buffer[ 16 ];
//Recommend clearing buffers located on the stack ( may be filled with rubbish data )
memset( buffer, 0x00, 16 ); //Not required for MemPrinter use.
//Class in action.
MemPrinter( buffer ).print( 123.45678f );
//Print result to see
//Serial.print( buffer );
MemPrinter( buffer ).print( 123.45678f, 5 );
//Serial.print( buffer );
Whether or not it’s “costly” really depends on what you are doing with it. If all you need to do is convert a number to its ASCII representation and put it in a string, then itoa() would be the easiest. If, however, you then need to take that ASCII number and combine it with another constant string or other ASCII numbers, then sprintf() is the best way to do it. The extra ~1.6KB in program memory isn’t going to be a problem unless you are right up against the limit in terms of program memory. You’re much more likely to run out of SRAM before your run out of program memory, though.
If you’re really concerned about efficiency, you can write the algorithm our yourself for your specific use.
KeithRB:
Pyro:
Have you checked to see if your libraries footprint is less than sprintf()?
No, but you will gain performance if you are using the print class anywhere else, such as 'Serial' or an LCD class. This is due to it sharing the same functionality, whereas sprintf may result in an additional algorithm for the same task done by Serial.
After a little testing, it seems quite an improvement over sprintf.
sprintf had an overhead of 1758 bytes, the Print class was only 816 bytes.
Below is a sketch using the code posted here
Test number 1 to 3 should compile the same size, test 0 is sprintf.
Floating point numbers are not included in this test as sprintf does not support them, whereas the print class handles floats fine.
MattS-UK:
I'm still trying to adapt my coding technique to the tiny amount of RAM available and I'm just a little nervous.
Using sprintf() only impacts RAM in the size of the string array you print to. The code goes to flash memory (which you have far more of, but that can get tight too), not RAM.