1

Which is better (in both performance and best practice) for printing only a newline:

putchar('\n') or puts("")

From what I understand, putchar outputs a single character and puts outputs a string of characters. Forget about printf.

I'm inclined to use puts because it's shorter, but doing ("") just feels wrong.

Which is faster and better?

Community
  • 1
  • 1
Insane
  • 889
  • 10
  • 21
  • 2
    so... `fputc` shouldn't be in that list of options?, or just `putc`? May as well include them all (`putchar` equivalent to `putc(ch,stdout)`) – WhozCraig Jan 14 '16 at 01:43
  • 2
    Personally, I'd prefer `putchar('\n')` but I don't have a good rational for it. It's just that I dislike `puts` because of its inconsistent behavior of adding a newline to the end while `fputs` does not. – 5gon12eder Jan 14 '16 at 01:44
  • 3
    You're worried about the *performance* of a buffered console that is going to be read by humans? If I told you that one of them was 8 microseconds faster than the other, would that be a reason to go for the one that serves up the newline to your waiting eyes that much faster? – Eric Lippert Jan 14 '16 at 01:45
  • 1
    Profile. But I strongly suspect that a macro whose sole purpose is to write a single character will do better (if only trivially) than a more general purpose function. Of course, if you were just using `puts` earlier, caching comes into play; hot code is likely faster than cold code even if the hot code has to execute a couple more instructions. – ShadowRanger Jan 14 '16 at 01:45
  • 1
    @EricLippert Not worried about it, curious about it. – Insane Jan 14 '16 at 01:46

4 Answers4

10

Any speed difference between them is going to be insignificant, but putchar is probably faster because it takes a single char argument.

Far more importantly, putchar('\n') is saying what you mean, while puts("") isn't.

hobbs
  • 223,387
  • 19
  • 210
  • 288
5

Theoretically, one requires a pointer, and an extra byte and the other doesn't. As well, one could require more instructions and potentially blow some i-cache, which could be bad. In practice the difference between the two is almost certainly negligible though.

However, I'd still personally use putc or putchar just because the code is easier for anyone else to read and understand.

Jason
  • 3,777
  • 14
  • 27
2

A good compiler will emit the same optimized code for the below, so it is not a performance issue.

putchar('\n');
puts("");

Use the one that better conveys code's intent. This often depends on what else is being printed.

// Example 1
putchar('(');
putchar(ch);
putchar(')');

putchar('\n');  // Better choice
// puts("");


// Example 2
puts(name);
puts(rank);
puts(serial_number);

// putchar('\n');  
puts(""); // Better choice
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

Even though puts() allows you to output a string, you are outputting a string that has only a single character (the newline), so the result, and the performance, should be the same as using putchar().