Pretty much.
When you redirect the stdout of the program to /dev/null
, any call to printf(3)
will still evaluate all the arguments, and the string formatting process will still take place before calling write(2)
, which writes the full formatted string to the standard output of the process. It's at the kernel level that the data isn't written to disk, but discarded by the handler associated with the special device /dev/null
.
So at the very best, you won't bypass or evade the overhead of evaluating the arguments and passing them to printf
, the string formatting job behind printf
, and at least one system call to actually write the data, just by redirecting stdout to /dev/null
. Well, that's a true difference on Linux. The implementation just returns the number of bytes you wanted to write (specified by the 3rd argument of your call to write(2)
) and ignores everything else (see this answer). Depending on the amount of data you're writing, and the speed of the target device (disk or terminal), the difference in performance may vary a lot. On embedded systems, generally speaking, cutting off the disk write by redirecting to /dev/null
can save quite some system resources for a non-trivial amount of written data.
Although in theory, the program could detect /dev/null
and perform some optimizations within the restrictions of standards they comply to (ISO C and POSIX), based on general understanding of common implementations, they practically don't (i.e. I am unaware of any Unix or Linux system doing so).
The POSIX standard mandates writing to the standard output for any call to printf(3)
, so it's not standard-conforming to suppress the call to write(2)
depending on the associated file descriptors. For more details about POSIX requirements, you can read Damon's answer. Oh, and a quick note: All Linux distros are practically POSIX-compliant, despite not being certified to be so.
Be aware that if you replace printf
completely, some side effects may go wrong, for example printf("%d%n", a++, &b)
. If you really need to suppress the output depending on the program execution environment, consider setting a global flag and wrap up printf to check the flag before printing — it isn't going to slow down the program to an extent where the performance loss is visible, as a single condition check is much faster than calling printf
and doing all the string formatting.
printf("%d", x=a+b);
If you redirect to /dev/null side effects will happen; if you rewrite as a do nothing macro, side effects will be lost – Leannaleannemyprintf(...) { return; }
is probably what you want. You can then have a macro for printf forwarding to that method, preserving side effects yet not formatting any string or calling write – Toluprintf
statement are evil. In code review, I'd definitely raise an issue over that. – Warmsprintf
still has to do the work of writing to it. – Dubrovnikprintf()
for logging ties the deamon to the output file selected at startup - for the life of the daemon process.printf()
output is also buffered, which means abnormal termination of a process leads to lost log entries - right when you need to see exactly what's going on. That's two more reasonsprintf()
is a very, very, very bad logging mechanism. :-) – Brisling