写入标准输出与写入文件
这类似于:
我很困惑,因为有两个冲突答案。我写了一个简单的java程序
I was confused because there are two conflicting answers. I wrote a simple java program
for(int i=0; i<1000000; i++){
System.out.println(i);
}
并以 / usr / bin / time运行-v java test
来测量输出到stdout的时间,然后我试过 / usr / bin / time -v java test> file
和 / usr / bin / time -v java>的/ dev / null的
。写入控制台最慢(10秒)然后文件(6秒)和 / dev / null
最快(2秒)。为什么?
and ran it with /usr/bin/time -v java test
to measure time to output to stdout, then I tried /usr/bin/time -v java test > file
and /usr/bin/time -v java > /dev/null
. Writing to console was slowest (10 seconds) then file (6 seconds) and /dev/null
was fastest (2 seconds). Why?
因为写入控制台需要在每次写入内容时刷新屏幕,这需要时间。
Because writing to console needs to refresh the screen each time something is written, which takes time.
写入文件需要在磁盘上写入字节,这需要时间,但比刷新屏幕的时间要短。
Writing to a file needs to write bytes on disk, which takes time, but less time than refreshing the screen.
写入 / dev / null
不会在任何地方写任何内容,这会花费更少的时间。
And writing to /dev/null
doesn't write anything anywhere, which takes much less time.