Java中的System.out.println()vs \ n [英] System.out.println() vs \n in Java

查看:312
本文介绍了Java中的System.out.println()vs \ n的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想打印5行。哪种方法最佳(性能和可读性)。

Let's say I wanted to print 5 lines. Which is the best method (for performance and readability).

System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println();

System.out.println("\n\n\n\n");

这是一个偏好的问题还是一个比另一个好的问题。看起来它会节省很多时间使用第二种方法。

Is it a matter of preference or is one better than the other. It seems like it would save a lot of time using the second method.

推荐答案

有一个功能两者之间的区别。第一个版本使用平台的首选行分隔符输出换行符。第二个版本输出换行符,这可能在Windows或Mac OS上不合适。

There is a functional difference between the two. The first version outputs line breaks using the platform's preferred line separator. The second version outputs newline characters, which is likely to be inappropriate on Windows or Mac OS.

这比任何真实或想象的性能优势更重要。

This is more important than any real or imagined performance advantages.

关于表现的主题,以及为什么每个人似乎都说已经足够。

On the topic of performance, and why everyone seems to be saying "enough already".

您编写该代码的两种方式之间的性能差异可能是少量的微秒或更少。换句话说,最终用户不会注意到差异......除非代码被执行了数百万次。

The performance difference between your two ways of writing that code is likely to be a small number of microseconds, or less. In other words, an end user won't notice the difference ... unless the code is executed millions of times.

作为一般规则,专业软件工程师采取认为如果不需要更快,那么就不值得花更多的时间来制作更快的东西。而肯定不值得花费客户的钱来做这件事。

As a general rule, professional software engineers take the view that it is not worth spending time to make something faster if it doesn't need to be faster. And it is certainly not worth spending the client's money doing this.

如果您有明确的证据表明存在或将会出现性能问题,您应该只沉迷于微优化,并且您要优化的代码是真正的问题在哪里。优化错误代码的时间浪费时间。

You should only indulge in micro-optimization if you have clear evidence that there is, or will be a performance problem, and that the code that you are about to optimize is where the real problem is / will be. Time spent optimizing the wrong bit of code is time wasted.

那么你怎么知道何时进行优化?

So how do you know when to optimize?


  • 当根据实际重要的标准衡量应用程序显着缓慢时。

  • When the application is observably slow when measured against criteria that actually matter.

你怎么知道要优化的什么


  • 通过运行应用程序分析器,并分析其输出,以查看实际性能热点和瓶颈的位置。

性能并不总是一个不重要的问题。实际上,对于某些类型的软件,没有考虑性能和可扩展性要求的设计或实现可能是一场灾难。但是,大多数软件都不是那样的。

Performance is not always an unimportant issue. Indeed, for some kinds of software, a design or implementation that doesn't take account of performance and scalability requirements can be a total disaster. However, most software is not like that.

这篇关于Java中的System.out.println()vs \ n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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