CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

printf or sprintf and puts

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
pfournier



Joined: 30 Sep 2003
Posts: 89

View user's profile Send private message

printf or sprintf and puts
PostPosted: Tue May 24, 2005 8:15 am     Reply with quote

Does anyone know right off the bat, which would consume the least amount of code space, a printf or a sprintf followed by a puts?
-Pete
_________________
-Pete
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 24, 2005 1:28 pm     Reply with quote

Compile some test files and look at the ROM usage that's given at the top
of the .LST file. You'll see something like this:
Quote:

ROM used: 22 words (0%)
Largest free fragment is 2048
RAM used: 6 (3%) at main() level
6 (3%) worst case
Stack: 0 locations

This tells you that this test program used 22 ROM locations.
pfournier



Joined: 30 Sep 2003
Posts: 89

View user's profile Send private message

PostPosted: Wed May 25, 2005 5:40 am     Reply with quote

I'll let you know what I find
_________________
-Pete
pfournier



Joined: 30 Sep 2003
Posts: 89

View user's profile Send private message

PostPosted: Thu May 26, 2005 10:11 am     Reply with quote

Here is what I ran.
Here are the code sizes.
With the simple printf it was 98, sprintf was 180
With the printf with a variable it was 344, sprintf was 426
I guess printf wins.
-Pete


void main()
{
char buffer[20];
char count=1;

//printf("test\n\r");
//sprintf(buffer, "test\n\r");
//puts(buffer);

//printf("Count %04d\n", Count);
sprintf(buffer, "Count %04d\n", Count);
puts(buffer);

}
_________________
-Pete
Ttelmah
Guest







PostPosted: Thu May 26, 2005 10:25 am     Reply with quote

It is what I'd 'expect, since with the sprintf, you are adding two sets of character array accesses, but it is nice to have it confirmed.

Best Wishes
pfournier



Joined: 30 Sep 2003
Posts: 89

View user's profile Send private message

PostPosted: Thu May 26, 2005 10:49 am     Reply with quote

This was interesting because I am told that with the Dynamic C compiler on the Rabbit processor the reverse is true. I gotta try that out when I get time.
_________________
-Pete
Ttelmah
Guest







PostPosted: Thu May 26, 2005 1:18 pm     Reply with quote

That might make sense, if the compiler uses a form of buffered I/O, and 'knows' to do a single memory copy from the string to the buffer. Or if the individual 'putc' commands used inside the printf, are 'inline', and appear in a lot of places. With a normal I/O structure it does not make sense.

Best Wishes
DragonPIC



Joined: 11 Nov 2003
Posts: 118

View user's profile Send private message

PostPosted: Fri May 27, 2005 1:05 pm     Reply with quote

Does the CCS compiler optimize when printf is used more often in the same program? Is there anything else that could cause printf to compile differently? You might have to do another test to confirm total use.
_________________
-Matt
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group