printf() vs. puts() 
Author Message
 printf() vs. puts()

Compiler: Microsoft Quick C 2.5
Computer: 486-33

      routine:      for(i = 0;i < 100; i++) printf("\n");

           time: 0.38 seconds

      routine:      for(i = 0;i < 100: i++) puts("\n");

           time: 0.77 seconds

Conclusion: printf is more efficient than puts.



Sun, 12 Apr 1998 03:00:00 GMT  
 printf() vs. puts()
|> Compiler: Microsoft Quick C 2.5
|> Computer: 486-33
|>
|>       routine:      for(i = 0;i < 100; i++) printf("\n");
|>            time: 0.38 seconds
|>
|>       routine:      for(i = 0;i < 100: i++) puts("\n");
|>            time: 0.77 seconds
|>
|> Conclusion: printf is more efficient than puts.

The only conclusion is that printing a single newline on with your compiler,
on your machine, via the printf() function is faster than printing two
newlines with your compiler, on your machine, via the puts() function.

Note I did say 2! By default puts() will tack on a newline to whatever is
printed. Therefore, if you print a single newline 2 will be printed.
In effect your second loop is doing twice the work! Divide your second
time by the this factor and you will see they are very close.

I would recommend you re-run your loops using 'fprintf (stdout, "\n");'
and 'fputs ("\n", stdout);'. Then you will not have to worry about
anything extra from either of the functions since each will still be
taking identical parameters and perform an equivalent operation.

Note that this is *NOT* a proof of efficiency unless you test your code
on every machine that exists with every C compiler that exists on all
optimization settings that are available. Even then your conclusions
may become invalid at a later date ;-).
--
"If it wasn't for C, we would be using BASI, PASAL, and OBOL."




Sun, 12 Apr 1998 03:00:00 GMT  
 printf() vs. puts()

Quote:

>Compiler: Microsoft Quick C 2.5
>Computer: 486-33

>      routine:      for(i = 0;i < 100; i++) printf("\n");

>           time: 0.38 seconds

>      routine:      for(i = 0;i < 100: i++) puts("\n");

>           time: 0.77 seconds

>Conclusion: printf is more efficient than puts.

Instead of:
      routine:      for(i = 0;i < 100: i++) puts("\n");
Should be
      routine:      for(i = 0;i < 100: i++) puts("");
for a fair comparision.  
Puts automatically emits a new line.  Your second routine
scrolls the console twice as far.
--
The opinions expressed in this message are my own personal views
and do not reflect the official views of Microsoft Corporation.


Mon, 13 Apr 1998 03:00:00 GMT  
 printf() vs. puts()

: Compiler: Microsoft Quick C 2.5
: Computer: 486-33

:       routine:      for(i = 0;i < 100; i++) printf("\n");

:            time: 0.38 seconds

:      

:       routine:      for(i = 0;i < 100: i++) puts("\n");

:            time: 0.77 seconds

: Conclusion: printf is more efficient than puts.

Not necessarily; it's not a fair test.

With each puts("\n"), you are putting at least 2 bytes to stdout; with
printf("\n") only half that many. Your MSDOS machine is probably doing
\r\n for the \n's.

Try using puts("") and see what the results are. Maybe even try a timing
for putchar('\n').

--



Tue, 14 Apr 1998 03:00:00 GMT  
 printf() vs. puts()

Quote:
>Compiler: Microsoft Quick C 2.5
>Computer: 486-33
>      routine:      for(i = 0;i < 100; i++) printf("\n");
>           time: 0.38 seconds

>      routine:      for(i = 0;i < 100: i++) puts("\n");
>           time: 0.77 seconds
>Conclusion: printf is more efficient than puts.

  That is sort of a leap of faith... also the puts() version is causing
twice as many newlines as the printf() version.  Perhaps you should
replace the 100 with 50 in the "puts() for loop" and rerun your test.
100 is also sort of small.  You are timing things that take less than 1
second.  Try larger values.... like 50000 or so.

--
Kent Vander Velden



Tue, 14 Apr 1998 03:00:00 GMT  
 printf() vs. puts()

Quote:

>>Compiler: Microsoft Quick C 2.5
>>Computer: 486-33

>>      routine:      for(i = 0;i < 100; i++) printf("\n");

>>           time: 0.38 seconds

>>      routine:      for(i = 0;i < 100: i++) puts("\n");

>>           time: 0.77 seconds

>>Conclusion: printf is more efficient than puts.

>  That is sort of a leap of faith... also the puts() version is causing
>twice as many newlines as the printf() version.  Perhaps you should
>replace the 100 with 50 in the "puts() for loop" and rerun your test.
>100 is also sort of small.  You are timing things that take less than 1
>second.  Try larger values.... like 50000 or so.

>--
>Kent Vander Velden


As has was suggested by many replys and posts, I ran again the test
as follows:

    where N = number of loops

for(i = 0;i < N;i++) printf("testing testing testing testing\n");

    and

for(i = 0; i < N;i++) puts("testing testing testing testing");

the results are:

  Function           N = 100   N = 1000    N = 5000
 -------------      --------   --------    --------
   puts             0.65 sec   6.92 sec    34.66 sec

   printf           0.66 sec   6.64 sec    33.12 sec

This would seem to indicate that there is not any significant
difference in speed between the two functions.



Thu, 16 Apr 1998 02:00:00 GMT  
 printf() vs. puts()

Quote:
>  Function           N = 100   N = 1000    N = 5000
> -------------      --------   --------    --------
>   puts             0.65 sec   6.92 sec    34.66 sec

>   printf           0.66 sec   6.64 sec    33.12 sec

>This would seem to indicate that there is not any significant
>difference in speed between the two functions.

Definitely not on a system where the scrolling of the screen is done
by the cpu, which has to move a few KB in the video memory for each
line scrolled.  This is what your test program is actually timing,
not the differences between puts and printf.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Fri, 17 Apr 1998 03:00:00 GMT  
 printf() vs. puts()

Quote:


>>  Function           N = 100   N = 1000    N = 5000
>> -------------      --------   --------    --------
>>   puts             0.65 sec   6.92 sec    34.66 sec

>>   printf           0.66 sec   6.64 sec    33.12 sec

>>This would seem to indicate that there is not any significant
>>difference in speed between the two functions.

>Definitely not on a system where the scrolling of the screen is done
>by the cpu, which has to move a few KB in the video memory for each
>line scrolled.  This is what your test program is actually timing,
>not the differences between puts and printf.

I did a bit more comparative testing on this. Of course the results very
little should be inferred from the results since they are only valid on
the particular environment they were run on. However they may dispell
some preconceptions people may have about performance of the various stdio
output function. FOr what its worth the platform is a 486/33 running SCO
Unix, compile with gcc -O2 but using the standard SCO libraries. The program
was run with:

ptest 1000000 > /dev/null

The program is:

------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define PTYPE 0            /* Select the printf function required */

int main(int argc, char *argv[])

{
    long count = atol(argv[1]);
    long ind;
    clock_t start_time, end_time;

    start_time = clock();
    for (ind = 0; ind < count; ind++) {

#if PTYPE == 0
        printf("testing testing testing testing\n");
#elif PTYPE == 1
        printf("%s", "testing testing testing testing\n");
#elif PTYPE == 2
        fprintf(stdout, "testing testing testing testing\n");
#elif PTYPE == 3
        puts("testing testing testing testing");
#elif PTYPE == 4
        fputs("testing testing testing testing\n", stdout);
#elif PTYPE == 5
        fwrite("testing testing testing testing\n", 1, 32, stdout);
#elif PTYPE == 6
        const char *ptr = "testing testing testing testing\n";

        for (; *ptr != '\0'; ptr++)
            putchar(*ptr);
#else
#error Illegal PTYPE
#endif

    }

    end_time = clock();

    fprintf(stderr, "Time = %.2f\n", (end_time-start_time) /
                                       (double)CLOCKS_PER_SEC);

    return 0;

Quote:
}

------------------------------------------------------------------------------

Results:

            | Run 1     Run 2     Run 3     Run 4
------------+------------------------------------
printf      | 20.36     20.19     20.61     20.30
            |
printf "%s" | 21.15     21.44     20.94     21.65
            |
fprintf     | 20.54     20.88     20.18     20.44
            |
puts        | 21.00     21.06     20.64     20.97
            |
fputs       | 21.71     21.69     21.76     21.88
            |
fwrite      |  9.71      9.55      9.20      9.80
            |
putchar     | 28.22     27.87     28.13     28.15

Apart from the last 2 cases the times are very similar. When writing out
to a file or device the overall times would be higher hence the differences
an even smaller proportion of the overall total.

Curiously here printf seems to be marginally faster than puts although
printf "%s" (for general strings) isn't.

putchar (which is a macro on this system) is somewhat slower than the
others but not disasterously so, especially of other I/O overhead
is taken into consideration.

fwrite is the surprise result being under half the times of the others.
I did check to confirm the output was the same as the others.

So while it is best to avoid character by character output where possible
there is no great reason to avoid any of the functions on performance grounds.
fwrite certainly shouldn't be forgotten.

--
-----------------------------------------


-----------------------------------------



Mon, 27 Apr 1998 03:00:00 GMT  
 printf() vs. puts()

Quote:
>Results:

>            | Run 1     Run 2     Run 3     Run 4
>------------+------------------------------------
>printf      | 20.36     20.19     20.61     20.30
>            |
>printf "%s" | 21.15     21.44     20.94     21.65
>            |
>fprintf     | 20.54     20.88     20.18     20.44
>            |
>puts        | 21.00     21.06     20.64     20.97
>            |
>fputs       | 21.71     21.69     21.76     21.88
>            |
>fwrite      |  9.71      9.55      9.20      9.80
>            |
>putchar     | 28.22     27.87     28.13     28.15

>Apart from the last 2 cases the times are very similar. When writing out
>to a file or device the overall times would be higher hence the differences
>an even smaller proportion of the overall total.

>Curiously here printf seems to be marginally faster than puts although
>printf "%s" (for general strings) isn't.

>putchar (which is a macro on this system) is somewhat slower than the
>others but not disasterously so, especially of other I/O overhead
>is taken into consideration.

>fwrite is the surprise result being under half the times of the others.
>I did check to confirm the output was the same as the others.

>So while it is best to avoid character by character output where possible
>there is no great reason to avoid any of the functions on performance grounds.
>fwrite certainly shouldn't be forgotten.

All your conclusions are valid _only_ for your implementation.  Which
makes them irrelevant for this newsgroup.

The point is that when you use printf("bla\n"), printf has to examine
every single character of the format string to see whether it is '%' or
not.  When you use puts("bla") this doesn't have to happen, so puts _can_
be implemented more efficiently than printf.  Which doesn't mean it will
be implemented more efficiently.  A brain dead implementation is free to
implement puts like this:

#include <stdio.h>

int puts(const char *s)
{
    return printf("%s\n", s);

Quote:
}

which is likely to be slower than printf("bla\n") because of the overhead
of the extra function call.

If using printf for all the output operations directed to stdout is a good
thing, why did the stdio designer provide putchar and puts?  Just to confuse
the newbie?  Some people still believe in the value of the principle
"use the right tool for the job".

The argument that using printf will make later changes easier, because you
have to grep for a single function name works for people who don't spend
any time designing their programs and start by coding them without any
kind of planning.  If it has been decided, during the design phase, that
the output has to go to stdout, it's highly unlikely that this will have
to be changed later.  If you just send the output to stdout without
thinking, you may find out later that this was not the right thing.
This explains why we see so many people asking how to temporarily
redirect stdout to a different stream.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Wed, 29 Apr 1998 03:00:00 GMT  
 printf() vs. puts()

Quote:

>All your conclusions are valid _only_ for your implementation.

I believe I said that in my original article.

Quote:
> Which makes them irrelevant for this newsgroup.

The point of my article was to show that performance isn't a major issue
in choosing one stdio stream function over another. People don't always
believe that unless they see some evidence of it so I thought I'd provide
some informal evidence.

Quote:
>The point is that when you use printf("bla\n"), printf has to examine
>every single character of the format string to see whether it is '%' or
>not.  When you use puts("bla") this doesn't have to happen, so puts _can_
>be implemented more efficiently than printf.  Which doesn't mean it will
>be implemented more efficiently.

It is quite easy to believe that printf is more efficient than puts, possibly
significantly so. I'm simply showing that at least 1 real (and not entirely
uncommon) system exists where that is not the case.

Quote:
>A brain dead implementation is free to
>implement puts like this:

>#include <stdio.h>

>int puts(const char *s)
>{
>    return printf("%s\n", s);
>}

>which is likely to be slower than printf("bla\n") because of the overhead
>of the extra function call.

Indeed. That might even be the case in this instance!

Quote:
>If using printf for all the output operations directed to stdout is a good
>thing, why did the stdio designer provide putchar and puts?  Just to confuse
>the newbie?  Some people still believe in the value of the principle
>"use the right tool for the job".

Exactly - I hoped to provide evidence that you can do that without undue
worries about performance.

Quote:
>The argument that using printf will make later changes easier, because you
>have to grep for a single function name works for people who don't spend
>any time designing their programs and start by coding them without any
>kind of planning.

I've argued against this approach in the past. If I wanted to change output
from stdout to a more general FILE * variable I would as a matter of course
grep for stdout and *all* standard streams output functions which direct
implicitly to stdout. It doesn't take very long and this sort of thoroughness
is essential to avoid bugs, even if you thought you only used printf.

--
-----------------------------------------


-----------------------------------------



Thu, 30 Apr 1998 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. (void)printf() vs. printf()

2. Re(2): Re(2): (void)printf() vs. printf()

3. Re(2): (void)printf() vs. printf()

4. printf() and puts()

5. printf, puts, gets etc don't work when stepping into

6. I/O with puts/printf

7. what is the difference between puts and printf ?

8. Why you should use puts(3) when you don't need printf(3)

9. why use puts instead of printf?

10. VC7: cstdio doesn't put printf in std

11. replacing printf with my printf, then calling C lib's printf

12. Question: Best place to put global variables on a VS.NET C# project

 

 
Powered by phpBB® Forum Software