View previous topic :: View next topic |
Author |
Message |
pfournier
Joined: 30 Sep 2003 Posts: 89
|
printf or sprintf and puts |
Posted: Tue May 24, 2005 8:15 am |
|
|
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
|
|
Posted: Tue May 24, 2005 1:28 pm |
|
|
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
|
|
Posted: Wed May 25, 2005 5:40 am |
|
|
I'll let you know what I find _________________ -Pete |
|
|
pfournier
Joined: 30 Sep 2003 Posts: 89
|
|
Posted: Thu May 26, 2005 10:11 am |
|
|
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
|
|
Posted: Thu May 26, 2005 10:25 am |
|
|
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
|
|
Posted: Thu May 26, 2005 10:49 am |
|
|
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
|
|
Posted: Thu May 26, 2005 1:18 pm |
|
|
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
|
|
Posted: Fri May 27, 2005 1:05 pm |
|
|
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 |
|
|
|