System.out.println() 与 Java 中的 \n
假设我想打印 5 行。这是最好的方法(对于性能和可读性)。
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println();
或者
System.out.println("\n\n\n\n");
这是一个偏好问题还是一个比另一个更好。看来使用第二种方法会节省很多时间。
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();
or
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
两者之间存在功能差异。第一个版本使用平台的首选行分隔符输出换行符。第二个版本输出换行符,这可能不适合 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?
And how do you know what to optimize?
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.
也许比其中一个更好:
它使用适合您平台的行分隔符,而
"\n"
则不使用。Perhaps better than either:
That uses the line separator appropriate to your platform, where
"\n"
doesn't.第二个肯定是代码更少,在性能方面您可能看不到差异,但后者可能更快,因为它只有 1 次调用,但可以忽略不计。
The 2nd one is definitely less code, performance wise you might not see a difference but the latter may be faster as it is only 1 call but it would be negligible.
从答案中可以看出,这是一个偏好问题。您给出的示例非常简单,其中性能提升和可读性问题很小。然而,在一些更复杂的情况下,选择使用哪些看似等效的方法可能会损害您的代码,或者可能使其无法被邻居解读。
如果代码看起来有点混乱,请尝试抽象出混乱的代码并为其指定一个易于阅读的方法名称。
所以是的,这是一个偏好问题。
(编辑:不,这显然不是一个偏好问题(^o^)//)
As you can see from the answers, this is a matter of preference. The example you give is a very simple one where the performance gains and readability issues are minimal. In some more complicated cases, however, the choice of which seemingly equivalent methods to use could be crippling to your code, or could make it indecipherable to a neighbor.
If code looks like it's getting kind of messy, try abstracting out the messy code and giving it an easy to read method name.
So yes, this is a matter of preference.
(Edit: no, this is apparently not a matter of preference (^o^ )// )
对于平台相关的行分隔符,我会使用:
这应该适用于 win、linux 或 mac。
For platform dependent line separator I would use:
That should work on win, linux or mac.
第二种选择是更好的方法。
它不仅对程序员来说更容易,而且可以减少函数开销。
The second option is the better method.
It is not only easier for the programmer but results in less function overhead.
第二个选项几乎肯定更快,因为每次调用 println() 都会调用系统函数。跳转到内核代码需要一些时间。这就是 C 中缓冲背后的原因。
The second option is almost certainly faster because each call to println() causes a system function to be called. This jump to kernel code takes some amount of time. This is the reasoning behind buffering in in C.
的输出是什么
System.out.println("\ /");
如果输出是 /,那么它不给出 \ / 的原因是什么?
what will be the output of
System.out.println("\ /");
If the output is /, then what is reason behind that it is not giving the out \ /?