2

Please I'd like to know which is better/quicker to process between:

printf("%s", foo);

and

for(int i = 0; i < len; i++)
    printf('%c', foo[i]);`

I have noticed that the output is not always the same.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
lvndry
  • 421
  • 6
  • 13
  • Why don't you time it and see? – Passer By Jul 03 '17 at 18:48
  • With any time functions? If you do not know how to time it, it is unlikely you should bother with the performance difference. – Passer By Jul 03 '17 at 18:52
  • Some compilers, including [GCC](http://gcc.gnu.org/) can optimize both your `printf` to something simpler. – Basile Starynkevitch Jul 03 '17 at 18:54
  • There is no definitive answer. It depends on many aspects of the compiler, library and processor. Both approaches have the same order of complexity. What is faster is to spend time coding higher levels concerns and that is where true performance gains can be had - not in this micro-optimization. – chux - Reinstate Monica Jul 03 '17 at 19:39
  • Use time function `clock()`. You can take a look at this discussion https://stackoverflow.com/questions/13156031/measuring-time-in-c – shamba Jul 03 '17 at 20:18
  • 1
    The `printf('%c', foo[i]);` call is erroneous; it should be `printf("%c", foo[i]);`, of course. Assuming that was fixed, then letting one call to `printf()` iterate over the string should be quicker than multiple calls to `printf()` for one character at a time. If you replace `printf("%c", foo[i]);` with `putchar(foo[i]);`, then maybe you'd be in with a chance, but `printf()` could use code which avoids the overhead of even that many calls to `putchar()` — whether `putchar()` is a macro or not. So, I'd be surprised to find that one call to `printf()` is slower than many calls to `printf()`. – Jonathan Leffler Jul 03 '17 at 20:55
  • In what way is the output not the same? Can you provide some examples? – Retired Ninja Jul 03 '17 at 20:59
  • Whty do you think that speed matters for this kind of code? This is usually dominated by the speed of your terminal, file system, whatever. If anything don't use `printf` at all if you simply want to output a string. `fputs` is your friend. – Jens Gustedt Jul 03 '17 at 21:12

4 Answers4

3

The single call printf("%s", foo); is most likely faster. You only make one function call to printf instead of n function calls, each of which has to parse the format string.

Even if the latter was faster, the former is still preferred because it is clearer to the reader.

dbush
  • 205,898
  • 23
  • 218
  • 273
2

Performance test it. Trying to come up with any rule that A is faster than B is bad practice. The assumption will become incorrect over time or in slightly different scenarios. Always performance test when optimizing.

Write a test case and test it with a high precision timer. Make sure your performance timer has a high enough granularity to show differences. On Windows you can use QueryPerformanceCounter. Googling that function should get you started.

Christopher Pisz
  • 3,757
  • 4
  • 29
  • 65
1

Note that there is a syntax error in: for(int i = 0; i < len; i++) printf('%c', foo[i]);. It should read:

int len = strlen(foo);
for (int i = 0; i < len; i++)
    printf("%c", foo[i]);

This loop, assuming the length of foo fits in a int, is quite inefficient. printf("%s", foo); is very likely faster than that, but if you have noticed different output, there is something fishy going on... Maybe len was not computed properly.

Note also that you could write a potentially more efficient version:

for (int i = 0; i < len; i++)
    putchar(foo[i]);

And you could also improve on printf("%s",foo); with this variant:

fputs(foo, stdout);

But as always, you should test and benchmark on your system to measure actual performance. Some compilers will optimize the printf("%s", foo); into fputs(foo, stdout); automatically.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • How do you print code like this please ? – lvndry Jul 03 '17 at 18:43
  • @LVNDRY: you can type code fragments between backticks (`) and indent code blocks by 4 spaces. Here is a complete guide: https://stackoverflow.com/editing-help – chqrlie Jul 03 '17 at 18:47
1

The statement without loop is probably faster than the statement with loop.

  1. Using for loop
    for(int i = 0; i < len ; i++) 
    printf("%c", foo[i]) ;

In every iteration, i is compared with 'len' and i is incremented after every character is printed.

  1. Without loop
    printf("%s", foo) ;

foo here is basically a pointer to string and it will go on printing every character after foo[0] until a '\0' character (or terminating character) is found. So, in the 2nd case, we save the time to update i and access foo[i] with respect to foo[0].

To add a disclaimer, the actual performance testing might lead you to results that would depend on the compiler and the operating system you are working on.

  • One could argue, that also printf probably loops over the string, incrementing some index or pointer until its length is reached and puts each character into the stdout buffer. The main performance gain is probably that `printf()` isn't called so often. But I doubt, that the `putchar(foo[i])`-version in a loop is much less performant in most implementations than `printf("%s", foo);`, despite the loop. – Ctx Jul 03 '17 at 19:04
  • @Ctx: the `putchar()` loop tends to be much less efficient than `printf` in some environments because the stream is locked/unlocked once per character instead of just once for `printf()`. – chqrlie Jul 03 '17 at 19:16