Will printf still have a cost even if I redirect output to /dev/null?
CLinuxPerformanceEmbeddedDev NullC Problem Overview
We have a daemon that contains a lot of print messages. Since we are working on an embedded device with a weak CPU and other constraint hardware, we want to minimize any kinds of costs (IO, CPU, etc..) of printf messages in our final version. (Users don't have a console)
My teammate and I have a disagreement. He thinks we can just redirect everything to /dev/null. It won't cost any IO so affections will be minimal. But I think it will still cost CPU and we better define a macro for printf so we can rewrite "printf" (maybe just return).
So I need some opinions about who is right. Will Linux be smart enough to optimize printf? I really doubt it.
C Solutions
Solution 1 - C
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.
Solution 2 - C
The printf
function will write to stdout
. It is not conforming to optimize for /dev/null
.
Therefore, you will have the overhead of parsing the format string and evaluating any necessary arguments, and you will have at least one syscall, plus you will copy a buffer to kernel address space (which, compared to the cost of the syscall is neglegible).
This answer is based on the specific documentation of POSIX.
>System Interfaces
dprintf, fprintf, printf, snprintf, sprintf - print formatted output
>The fprintf() function shall place output on the named output stream. The printf() function shall place output on the standard output stream stdout. The sprintf() function shall place output followed by the null byte, '\0', in consecutive bytes starting at *s; it is the user's responsibility to ensure that enough space is available.
>Base Definitions
> shall
For an implementation that conforms to POSIX.1-2017, describes a feature or behavior that is mandatory. An application can rely on the existence of the feature or behavior.
Solution 3 - C
The printf
function writes to stdout
. If the file descriptor connected to stdout
is redirected to /dev/null
then no output will be written anywhere (but it will still be written), but the call to printf
itself and the formatting it does will still happen.
Solution 4 - C
Write your own that wraps printf() using the printf() source as a guideline, and returning immediately if a noprint flag is set. The downside of this is when actually printing it will consume more resources because of having to parse the format string twice. But it uses negligible resources when not printing. Can't simply replace printf() because the underlying calls inside printf() can change with a newer version of the stdio library.
void printf2(const char *formatstring, ...);
Solution 5 - C
Generally speaking, an implementation is permitted to perform such optimisations if they do not affect the observable (functional) outputs of the program. In the case of printf()
, that would mean that if the program doesn't use the return value, and if there are no %n
conversions, then the implementation would be allowed to do nothing.
In practice, I'm not aware of any implementation on Linux that currently (early 2019) performs such an optimisation - the compilers and libraries I'm familiar with will format the output and write the result to the null device, relying on the kernel' to ignore it.
You may want to write a forwarding function of your own if you really need to save the cost of formatting when the output is not used - you'll want to it to return void
, and you should check the format string for %n
. (You could use snprintf
with a NULL
and 0
buffer if you need those side-effects, but the savings are unlikely to repay the effort invested).
Solution 6 - C
in C writing 0;
does execute and nothing, which is similar to ;
.
means you can write a macro like
#if DEBUG
#define devlognix(frmt,...) fprintf(stderr,(frmt).UTF8String,##__VA_ARGS__)
#else
#define nadanix 0
#define devlognix(frmt,...) nadanix
#endif
#define XYZKitLogError(frmt, ...) devlognix(frmt)
where XYZKitLogError
would be your Log command.
or even
#define nadanix ;
which will kick out all log calls at compile time and replace with 0;
or ;
so it gets parsed out.
you will get Unused variable warnings, but it does what you want and this side effect can even be helpful because it tells you about computation that is not needed in your release.
.UTF8String is an Objective-C method converting NSStrings to const char* - you don't need.