写入标准输出与写入文件 [英] Writing to standard out vs writing to file

查看:125
本文介绍了写入标准输出与写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这类似于:

打印到控制台与写入文件(速度)

我很困惑,因为有两个冲突答案。我写了一个简单的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.

这篇关于写入标准输出与写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆