You are not logged in.
Hi,
My question is fundamentally about speed difference between these two
In programming, printing the output is relatively a resource-hugging action in comparison to non-outputing, Just as a loop can run milions of times within seconds or even less, But when you try to print something in each cycle of that loop, it can take about minutes and can lead to crashes
Now my question is, how is this in linux ? What exactly happens when we redirect the output of a command to /dev/null instead of display ? Do any processes get executed on it ? Can it have performance improvements in some cases ?
There are some sources that when you start building them, they just generate output of every single line of code they are building, I have seen that these builds usually take about 10-20mins based on the source code, I always assume that they would take fewer minutes if they just STOP Printing every single line of code they are processing , What is your idea ?
Last edited by erfanjoker (2020-12-23 13:39:10)
Offline
Redirecting the output to a(nother) file can have impact on the program's execution time, yes.
It depends on how fast data can be written to the respective file.
Since /dev/null ist merely discarding all data written to it, it is insanely fast:
#include <stdio.h>
int main () {
unsigned int i;
for (i = 0; i <= 1000000; i++) {
fprintf( stdout, "Garbage to stdout.\n" );
fprintf( stderr, "Garbage to stderr.\n" );
}
}Results:
time ./test
real 0m6,518s
user 0m0,380s
sys 0m3,399s
time ./test > /dev/null
real 0m3,064s
user 0m0,205s
sys 0m1,635s
time ./test 2> /dev/null
real 0m3,586s
user 0m0,269s
sys 0m1,885s
time ./test 2> /dev/null >&2
real 0m0,173s
user 0m0,070s
sys 0m0,102sLast edited by schard (2020-12-23 08:52:08)
Inofficial first vice president of the Rust Evangelism Strike Force
Offline
First off all:
> /dev/null 2>&1The order matters.
https://www.gnu.org/software/bash/manua … tions.html
https://catonmat.net/bash-one-liners-ex … part-three
try to print something in each cycle of that loop, it can take about minutes and can lead to crashes
No. That's your code. Or a crap output (terminal)
To answer your question, you save the CPU on the terminal printing stuff and for compiler output that doesn't matter (unless, again, very crappy VTE)
Redirecting stdout and esp. stderr of a compiler to /dev/null is not the best of all ideas because you'll miss warnings and, more importantly, comments why yor code is broken and won't compile.
If there's really sth. that
generate(s) output of every single line of code they are building
there's probably a quiet parameter to restrict that behavior what would likely provide more performance than just redirecting the output.
Online
Thanks all, I got the answer
Offline