Will printf still has a cost even I redirect output to /dev/null?
We have a daemon contains a lot of print message. Since we working on a embedded device with a weak CPU and other constraint hardware, we want to minimize any kinds of costs(IO, CPU,etc..) of printf message in our final version. (Users don't have a console)
My teammates and I have a disagreement. He think 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 smart enough to optimize printf? I really doubt it.
c linux
add a comment |
We have a daemon contains a lot of print message. Since we working on a embedded device with a weak CPU and other constraint hardware, we want to minimize any kinds of costs(IO, CPU,etc..) of printf message in our final version. (Users don't have a console)
My teammates and I have a disagreement. He think 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 smart enough to optimize printf? I really doubt it.
c linux
2
Beware side effects: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
– pmg
3 hours ago
1
Providing amyprintf(...) { 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
– msrd0
3 hours ago
add a comment |
We have a daemon contains a lot of print message. Since we working on a embedded device with a weak CPU and other constraint hardware, we want to minimize any kinds of costs(IO, CPU,etc..) of printf message in our final version. (Users don't have a console)
My teammates and I have a disagreement. He think 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 smart enough to optimize printf? I really doubt it.
c linux
We have a daemon contains a lot of print message. Since we working on a embedded device with a weak CPU and other constraint hardware, we want to minimize any kinds of costs(IO, CPU,etc..) of printf message in our final version. (Users don't have a console)
My teammates and I have a disagreement. He think 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 smart enough to optimize printf? I really doubt it.
c linux
c linux
asked 3 hours ago
Michael PengMichael Peng
1189
1189
2
Beware side effects: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
– pmg
3 hours ago
1
Providing amyprintf(...) { 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
– msrd0
3 hours ago
add a comment |
2
Beware side effects: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
– pmg
3 hours ago
1
Providing amyprintf(...) { 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
– msrd0
3 hours ago
2
2
Beware side effects:
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– pmg
3 hours ago
Beware side effects:
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– pmg
3 hours ago
1
1
Providing a
myprintf(...) { 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– msrd0
3 hours ago
Providing a
myprintf(...) { 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– msrd0
3 hours ago
add a comment |
4 Answers
4
active
oldest
votes
Pretty much.
Although in theory, the program could detect /dev/null
and perform some optimizations within the restrictions of standards they comply to, based on general understanding of common implementations, they practically don't (i.e. I am unaware of any Unix or Linux system doing so). You can read Damon's answer for details about POSIX standard requirements.
When you redirect the stdout of the program to /dev/null
, any call to printf(3)
will still evaluate all the arguments (beware side effects like a++
), and the string formatting 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
. Therefore I see little improvement in terms of performance, unless your disk or terminal is considerably slow. Disk and RAM capacity (for terminals) may be another concern, though.
Be aware that if you replace printf
completely, some side effects may go wrong, for example printf("%d%n", a++, &b)
.
4
Answers like this should state they are based on general understanding of common implementations and not upon specific documentation. In theory, there is no reason a C implementation might not inspectstdout
, learn it is /dev/null, and suppressprintf
calls that do not contain%n
and whose return value is not used. We cannot really assert nobody has done this, and students ought to learn the provenance of information since an important part of engineering is knowing how you know something (is it specified in a standard, is it just assumed, is it provable, and so on).
– Eric Postpischil
1 hour ago
1
@EricPostpischil Thanks for that! Very valuable information.
– iBug
1 hour ago
add a comment |
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.
1
@OP: Addition: You can further reduce the cost ofprintf()
by creating a new driver which provides a newFILE *
(depending on if your platform supports that). In this case, you can create a data sink which discards the data. The cost for formatting etc. still remains, but the OS call for writing to/dev/null
goes away.
– glglgl
3 hours ago
1
Answers like this should state they are based on general understanding of common implementations and not upon specific documentation. In theory, there is no reason a C implementation might not inspectstdout
, learn it is /dev/null, and suppressprintf
calls that do not contain%n
and whose return value is not used. We cannot really assert nobody has done this, and students ought to learn the provenance of information since an important part of engineering is knowing how you know something (is it specified in a standard, is it just assumed, is it provable, and so on).
– Eric Postpischil
1 hour ago
add a comment |
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).
add a comment |
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, '', 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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54196197%2fwill-printf-still-has-a-cost-even-i-redirect-output-to-dev-null%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Pretty much.
Although in theory, the program could detect /dev/null
and perform some optimizations within the restrictions of standards they comply to, based on general understanding of common implementations, they practically don't (i.e. I am unaware of any Unix or Linux system doing so). You can read Damon's answer for details about POSIX standard requirements.
When you redirect the stdout of the program to /dev/null
, any call to printf(3)
will still evaluate all the arguments (beware side effects like a++
), and the string formatting 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
. Therefore I see little improvement in terms of performance, unless your disk or terminal is considerably slow. Disk and RAM capacity (for terminals) may be another concern, though.
Be aware that if you replace printf
completely, some side effects may go wrong, for example printf("%d%n", a++, &b)
.
4
Answers like this should state they are based on general understanding of common implementations and not upon specific documentation. In theory, there is no reason a C implementation might not inspectstdout
, learn it is /dev/null, and suppressprintf
calls that do not contain%n
and whose return value is not used. We cannot really assert nobody has done this, and students ought to learn the provenance of information since an important part of engineering is knowing how you know something (is it specified in a standard, is it just assumed, is it provable, and so on).
– Eric Postpischil
1 hour ago
1
@EricPostpischil Thanks for that! Very valuable information.
– iBug
1 hour ago
add a comment |
Pretty much.
Although in theory, the program could detect /dev/null
and perform some optimizations within the restrictions of standards they comply to, based on general understanding of common implementations, they practically don't (i.e. I am unaware of any Unix or Linux system doing so). You can read Damon's answer for details about POSIX standard requirements.
When you redirect the stdout of the program to /dev/null
, any call to printf(3)
will still evaluate all the arguments (beware side effects like a++
), and the string formatting 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
. Therefore I see little improvement in terms of performance, unless your disk or terminal is considerably slow. Disk and RAM capacity (for terminals) may be another concern, though.
Be aware that if you replace printf
completely, some side effects may go wrong, for example printf("%d%n", a++, &b)
.
4
Answers like this should state they are based on general understanding of common implementations and not upon specific documentation. In theory, there is no reason a C implementation might not inspectstdout
, learn it is /dev/null, and suppressprintf
calls that do not contain%n
and whose return value is not used. We cannot really assert nobody has done this, and students ought to learn the provenance of information since an important part of engineering is knowing how you know something (is it specified in a standard, is it just assumed, is it provable, and so on).
– Eric Postpischil
1 hour ago
1
@EricPostpischil Thanks for that! Very valuable information.
– iBug
1 hour ago
add a comment |
Pretty much.
Although in theory, the program could detect /dev/null
and perform some optimizations within the restrictions of standards they comply to, based on general understanding of common implementations, they practically don't (i.e. I am unaware of any Unix or Linux system doing so). You can read Damon's answer for details about POSIX standard requirements.
When you redirect the stdout of the program to /dev/null
, any call to printf(3)
will still evaluate all the arguments (beware side effects like a++
), and the string formatting 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
. Therefore I see little improvement in terms of performance, unless your disk or terminal is considerably slow. Disk and RAM capacity (for terminals) may be another concern, though.
Be aware that if you replace printf
completely, some side effects may go wrong, for example printf("%d%n", a++, &b)
.
Pretty much.
Although in theory, the program could detect /dev/null
and perform some optimizations within the restrictions of standards they comply to, based on general understanding of common implementations, they practically don't (i.e. I am unaware of any Unix or Linux system doing so). You can read Damon's answer for details about POSIX standard requirements.
When you redirect the stdout of the program to /dev/null
, any call to printf(3)
will still evaluate all the arguments (beware side effects like a++
), and the string formatting 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
. Therefore I see little improvement in terms of performance, unless your disk or terminal is considerably slow. Disk and RAM capacity (for terminals) may be another concern, though.
Be aware that if you replace printf
completely, some side effects may go wrong, for example printf("%d%n", a++, &b)
.
edited 1 min ago
answered 3 hours ago
iBugiBug
19.4k53363
19.4k53363
4
Answers like this should state they are based on general understanding of common implementations and not upon specific documentation. In theory, there is no reason a C implementation might not inspectstdout
, learn it is /dev/null, and suppressprintf
calls that do not contain%n
and whose return value is not used. We cannot really assert nobody has done this, and students ought to learn the provenance of information since an important part of engineering is knowing how you know something (is it specified in a standard, is it just assumed, is it provable, and so on).
– Eric Postpischil
1 hour ago
1
@EricPostpischil Thanks for that! Very valuable information.
– iBug
1 hour ago
add a comment |
4
Answers like this should state they are based on general understanding of common implementations and not upon specific documentation. In theory, there is no reason a C implementation might not inspectstdout
, learn it is /dev/null, and suppressprintf
calls that do not contain%n
and whose return value is not used. We cannot really assert nobody has done this, and students ought to learn the provenance of information since an important part of engineering is knowing how you know something (is it specified in a standard, is it just assumed, is it provable, and so on).
– Eric Postpischil
1 hour ago
1
@EricPostpischil Thanks for that! Very valuable information.
– iBug
1 hour ago
4
4
Answers like this should state they are based on general understanding of common implementations and not upon specific documentation. In theory, there is no reason a C implementation might not inspect
stdout
, learn it is /dev/null, and suppress printf
calls that do not contain %n
and whose return value is not used. We cannot really assert nobody has done this, and students ought to learn the provenance of information since an important part of engineering is knowing how you know something (is it specified in a standard, is it just assumed, is it provable, and so on).– Eric Postpischil
1 hour ago
Answers like this should state they are based on general understanding of common implementations and not upon specific documentation. In theory, there is no reason a C implementation might not inspect
stdout
, learn it is /dev/null, and suppress printf
calls that do not contain %n
and whose return value is not used. We cannot really assert nobody has done this, and students ought to learn the provenance of information since an important part of engineering is knowing how you know something (is it specified in a standard, is it just assumed, is it provable, and so on).– Eric Postpischil
1 hour ago
1
1
@EricPostpischil Thanks for that! Very valuable information.
– iBug
1 hour ago
@EricPostpischil Thanks for that! Very valuable information.
– iBug
1 hour ago
add a comment |
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.
1
@OP: Addition: You can further reduce the cost ofprintf()
by creating a new driver which provides a newFILE *
(depending on if your platform supports that). In this case, you can create a data sink which discards the data. The cost for formatting etc. still remains, but the OS call for writing to/dev/null
goes away.
– glglgl
3 hours ago
1
Answers like this should state they are based on general understanding of common implementations and not upon specific documentation. In theory, there is no reason a C implementation might not inspectstdout
, learn it is /dev/null, and suppressprintf
calls that do not contain%n
and whose return value is not used. We cannot really assert nobody has done this, and students ought to learn the provenance of information since an important part of engineering is knowing how you know something (is it specified in a standard, is it just assumed, is it provable, and so on).
– Eric Postpischil
1 hour ago
add a comment |
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.
1
@OP: Addition: You can further reduce the cost ofprintf()
by creating a new driver which provides a newFILE *
(depending on if your platform supports that). In this case, you can create a data sink which discards the data. The cost for formatting etc. still remains, but the OS call for writing to/dev/null
goes away.
– glglgl
3 hours ago
1
Answers like this should state they are based on general understanding of common implementations and not upon specific documentation. In theory, there is no reason a C implementation might not inspectstdout
, learn it is /dev/null, and suppressprintf
calls that do not contain%n
and whose return value is not used. We cannot really assert nobody has done this, and students ought to learn the provenance of information since an important part of engineering is knowing how you know something (is it specified in a standard, is it just assumed, is it provable, and so on).
– Eric Postpischil
1 hour ago
add a comment |
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.
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.
edited 3 hours ago
answered 3 hours ago
Some programmer dudeSome programmer dude
296k24250411
296k24250411
1
@OP: Addition: You can further reduce the cost ofprintf()
by creating a new driver which provides a newFILE *
(depending on if your platform supports that). In this case, you can create a data sink which discards the data. The cost for formatting etc. still remains, but the OS call for writing to/dev/null
goes away.
– glglgl
3 hours ago
1
Answers like this should state they are based on general understanding of common implementations and not upon specific documentation. In theory, there is no reason a C implementation might not inspectstdout
, learn it is /dev/null, and suppressprintf
calls that do not contain%n
and whose return value is not used. We cannot really assert nobody has done this, and students ought to learn the provenance of information since an important part of engineering is knowing how you know something (is it specified in a standard, is it just assumed, is it provable, and so on).
– Eric Postpischil
1 hour ago
add a comment |
1
@OP: Addition: You can further reduce the cost ofprintf()
by creating a new driver which provides a newFILE *
(depending on if your platform supports that). In this case, you can create a data sink which discards the data. The cost for formatting etc. still remains, but the OS call for writing to/dev/null
goes away.
– glglgl
3 hours ago
1
Answers like this should state they are based on general understanding of common implementations and not upon specific documentation. In theory, there is no reason a C implementation might not inspectstdout
, learn it is /dev/null, and suppressprintf
calls that do not contain%n
and whose return value is not used. We cannot really assert nobody has done this, and students ought to learn the provenance of information since an important part of engineering is knowing how you know something (is it specified in a standard, is it just assumed, is it provable, and so on).
– Eric Postpischil
1 hour ago
1
1
@OP: Addition: You can further reduce the cost of
printf()
by creating a new driver which provides a new FILE *
(depending on if your platform supports that). In this case, you can create a data sink which discards the data. The cost for formatting etc. still remains, but the OS call for writing to /dev/null
goes away.– glglgl
3 hours ago
@OP: Addition: You can further reduce the cost of
printf()
by creating a new driver which provides a new FILE *
(depending on if your platform supports that). In this case, you can create a data sink which discards the data. The cost for formatting etc. still remains, but the OS call for writing to /dev/null
goes away.– glglgl
3 hours ago
1
1
Answers like this should state they are based on general understanding of common implementations and not upon specific documentation. In theory, there is no reason a C implementation might not inspect
stdout
, learn it is /dev/null, and suppress printf
calls that do not contain %n
and whose return value is not used. We cannot really assert nobody has done this, and students ought to learn the provenance of information since an important part of engineering is knowing how you know something (is it specified in a standard, is it just assumed, is it provable, and so on).– Eric Postpischil
1 hour ago
Answers like this should state they are based on general understanding of common implementations and not upon specific documentation. In theory, there is no reason a C implementation might not inspect
stdout
, learn it is /dev/null, and suppress printf
calls that do not contain %n
and whose return value is not used. We cannot really assert nobody has done this, and students ought to learn the provenance of information since an important part of engineering is knowing how you know something (is it specified in a standard, is it just assumed, is it provable, and so on).– Eric Postpischil
1 hour ago
add a comment |
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).
add a comment |
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).
add a comment |
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).
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).
answered 10 mins ago
Toby SpeightToby Speight
16.4k133965
16.4k133965
add a comment |
add a comment |
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, '', 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.
add a comment |
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, '', 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.
add a comment |
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, '', 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.
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, '', 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.
answered 8 mins ago
DamonDamon
50.8k1599153
50.8k1599153
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54196197%2fwill-printf-still-has-a-cost-even-i-redirect-output-to-dev-null%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Beware side effects:
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– pmg
3 hours ago
1
Providing a
myprintf(...) { 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– msrd0
3 hours ago