Java system.out 与新的 PrintStream

发布于 2024-09-17 23:20:48 字数 239 浏览 5 评论 0原文

如果要输出 15,000 行的消息,什么会使用更多内存?

System.out.println 会占用更多内存吗? 或者 System.setErr(new PrintStream(new FileOutputStream("tonsoflines.log"))) 会使用更多内存吗?

什么使用更多进程或需要更多内存才能运行?控制台窗口中的可视化输出或写入没有控制台可视化的文件?

编辑:还有哪一个会先完成消息?

What would use more memory if it were to output a message that was 15,000 lines?

Would System.out.println us more memory?
or Would System.setErr(new PrintStream(new FileOutputStream("tonsoflines.log"))) use more memory?

What uses more process or requires more memory to function? Visual output in a console window or writing to a file with no console visual?

Edit: Also which one would finish the message first?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九局 2024-09-24 23:20:48

控制台

作为一些测试:打印到 System.out 结果是:

Printing line : #0
.
.
.
Printing line : #14997
Printing line : #14998
Printing line : #14999
System.out took: 238ms
  • 测试#1:238ms
  • 测试#2:235ms
  • 测试#3:251ms
  • 测试#4:210ms
  • 测试#5:172ms
  • 测试#6:268ms

new Stream

并使用新的Stream结果是:

Printing line : #0
.
.
.
Printing line : #14996
Printing line : #14997
Printing line : #14998
Printing line : #14999
System.out with new stream took: 220ms
  • 测试#1:220ms
  • 测试#2:219ms
  • 测试#3:222ms
  • 测试#4:220ms code>
  • 测试#5:223ms
  • 测试#6:250ms

代码

    try {

        System.setOut(new PrintStream(new FileOutputStream("C:\\TMP\\logs.log")));
                    long startTime = Calendar.getInstance().getTimeInMillis();
        for (int a=0;a<15000;a++) {
            System.out.println("Printing line : #"+a);
        }

        long elapsedTime = (Calendar.getInstance().getTimeInMillis() - startTime);
        System.out.println("System.out took: " + elapsedTime + "ms");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

如您所见,速度差异甚至不是“交易”,我没有检查的是使用这些方法的性能(磁盘 I/O、内存和 CPU)。

但如果你问我我的想法,我会告诉你结果将类似于我们刚刚执行的速度测试......对于我编码的任何应用程序中的日志,我使用 Log4J ( http://logging.apache.org/log4j/1.2/index.html )来记录我想要的任何内容,您可以设置您想要的登录级别(调试、信息、错误) , ETC)

Console

As some test: printing to System.out the result is:

Printing line : #0
.
.
.
Printing line : #14997
Printing line : #14998
Printing line : #14999
System.out took: 238ms
  • Test #1: 238ms
  • Test #2: 235ms
  • Test #3: 251ms
  • Test #4: 210ms
  • Test #5: 172ms
  • Test #6: 268ms

new Stream

And using a new Stream the result is:

Printing line : #0
.
.
.
Printing line : #14996
Printing line : #14997
Printing line : #14998
Printing line : #14999
System.out with new stream took: 220ms
  • Test #1: 220ms
  • Test #2: 219ms
  • Test #3: 222ms
  • Test #4: 220ms
  • Test #5: 223ms
  • Test #6: 250ms

Code

    try {

        System.setOut(new PrintStream(new FileOutputStream("C:\\TMP\\logs.log")));
                    long startTime = Calendar.getInstance().getTimeInMillis();
        for (int a=0;a<15000;a++) {
            System.out.println("Printing line : #"+a);
        }

        long elapsedTime = (Calendar.getInstance().getTimeInMillis() - startTime);
        System.out.println("System.out took: " + elapsedTime + "ms");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

As you see the difference in speed is not a even a "deal", What I didn't check was the performance (Disk I/O, Memory and CPU) using those methods.

But if you ask me about what i Think, i will tell that the result will be something like the speed test that we just performed... For my logs in any application i code, i use Log4J ( http://logging.apache.org/log4j/1.2/index.html ) to log anything i want and you can set the level of loggin you want (debug, info, error, etc)

所有深爱都是秘密 2024-09-24 23:20:48

我认为就内存而言,差异很小,可能比 System.out.println 稍有优势
就处理和速度而言,写入日志文件比在屏幕上打印所有内容要快得多。
大多数 Java 日志框架都针对日志操作期间的最大性能进行了优化,因此您肯定希望使用一个好的日志框架(例如 Log4J)来实现这一点。

I think in terms of memory the difference will be minimal, maybe a slight advantage to System.out.println
In terms of processing and speed, writing to a log file will be a lot faster that printing everything out on the screen.
Most logging frameworks for Java are optimized for maximum performance during logging operations, so you would definitely want to use a good logging framework such as Log4J for that.

柳若烟 2024-09-24 23:20:48

System.out是PrintStream的对象。所以我认为不会有太大区别。

http://download.oracle。 com/javase/1.4.2/docs/api/java/lang/System.html#out

公共静态最终PrintStream输出

“标准”输出流。这
流已经打开并准备好
接受输出数据。通常这个
流对应显示输出
或另一个输出目的地
由主机环境指定或
用户。

如果您正在考虑将其用于日志记录,则始终建议使用专门的日志记录框架,例如 Log4J

System.out is object of PrintStream. So I don't think there would be much difference.

http://download.oracle.com/javase/1.4.2/docs/api/java/lang/System.html#out

public static final PrintStream out

The "standard" output stream. This
stream is already open and ready to
accept output data. Typically this
stream corresponds to display output
or another output destination
specified by the host environment or
user.

If you are considering this for logging, It would always be recomended to use specialized logging framework like Log4J.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文