简体   繁体   English

System.out.println()是否会影响代码效率?

[英]Does System.out.println() effect the code efficiency ?

I used to write & develop codes with system.out.println() . 我曾经用system.out.println()编写和开发代码。 it usually helps me track values and where the problem is coming. 它通常可以帮助我跟踪价值和问题的来源。 And after developed application, I don't remove the system.out.println() because it might helpful after user found any issue it will come back to us, which makes it so easy to track where went to wrong. 在开发应用程序之后,我不会删除system.out.println()因为它可能会在用户发现任何问题后有用,它会回复给我们,这样可以很容易地跟踪出错的地方。 But one of my superior suggested to remove system.out.println() from codes because its effect the code efficient level. 但我的一位高级建议从代码中删除system.out.println() ,因为它影响了代码的有效性。 Is this correct? 它是否正确?

From my point of view, System.out.print() hardly take bytes on memory, so is it true that developer shouldn't use much system.out.println ?? 从我的角度来看, System.out.print()几乎不占用内存中的字节,因此开发人员不应该使用太多的system.out.println吗?

Thanks in advance. 提前致谢。

System.out implementation contains a synchronized block on the output stream. System.out实现包含输出流上的synchronized块。

From PrintStream.java : 来自PrintStream.java

      /**
        * Prints a String and then terminate the line.  This method behaves as
        * though it invokes <code>{@link #print(String)}</code> and then
        * <code>{@link #println()}</code>.
        *
        * @param x  The <code>String</code> to be printed.
        */

       public void println(String x) {
           synchronized (this) {
               print(x);
               newLine();
           }
      }

Putting System.out.println a lot will make the entire project to run almost single-threaded, because all thread will waiting for synchronization lock, and make your application begin to crawl. 大量放入System.out.println将使整个项目几乎单线程运行,因为所有线程都将等待同步锁定,并使您的应用程序开始爬行。

This means your superior is right. 这意味着你的上司是对的。

Alternatively, Use logging framework like log4j instead. 或者,使用log4j类的日志框架。 You can still configure your log4j to still output to System.out.println by just minor changes in the appender configuration. 您仍然可以通过appender配置中的微小更改将log4j配置为仍然输出到System.out.println。

You can use logger to solve the problem. 您可以使用记录器来解决问题。

Specify the level of logging while developing your code, and you can set level of logging in production. 在开发代码时指定日志记录级别,您可以设置生产中的日志记录级别。

List of levels: 等级列表:

DEBUG Level

INFO Level

WARN Level

ERROR Level

FATAL Level

Log4j is common one to use, see here: Log4j是常用的,请看这里:

http://logging.apache.org/log4j/2.x/ http://logging.apache.org/log4j/2.x/

Regarding the cons of using println: 关于使用println的缺点

The primary reason against is that println slows down the speed of your program. 反对的主要原因是println 会降低程序的速度。

use of logging frameworks such as SLF4J is highly recommended. 强烈建议使用SLF4J等日志框架。 That way by configuration, you can execute these logging statements based on the environement. 通过配置,您可以根据环境执行这些日志记录语句。

eg - DEBUG level on Developer -- WARN on Production, so only critical iformation is logged in production. 例如 - 开发人员的DEBUG级别 - 生产时的WARN,因此在生产中仅记录关键的iformation。

It is true that outputting to the console, especially excessive output, will reduce the application's performance. 确实,输出到控制台,特别是输出过多,会降低应用程序的性能。 It's also better programming style not to burden the user with information that only you can use. 它也是更好的编程风格,不会给用户带来只有你可以使用的信息。 Instead, you should use Java's in-built mechanisms for handling unexpected events, eg exceptions and the assert mechanism. 相反,您应该使用Java的内置机制来处理意外事件,例如异常和断言机制。 You can also choose to have a 'verbose' option as a command-line argument. 您还可以选择将“详细”选项作为命令行参数。

System.out.println can cause serious performance issues if used extensively in a multi-threaded environment (even webapplications). 如果在多线程环境(甚至是Web应用程序)中广泛使用,System.out.println可能会导致严重的性能问题。 Every println call acquires a lock so that message displayed on the console is not mixed up with other messages. 每个println调用都获取一个锁,以便控制台上显示的消息不会与其他消息混淆。

Also in most cases the output of the console is redirected to a file. 此外,在大多数情况下,控制台的输出会重定向到文件。 So it incurs a lot of synchronous IO operations that can lead to performance degradation. 因此,它会导致许多同步IO操作,从而导致性能下降。

Yes it is true that calls to System.out.println() must be minimized. 是的,必须最小化对System.out.println()的调用。 This is because printing to the console is an IO operation and IO take a relatively long time to complete. 这是因为打印到控制台是IO操作,IO需要相对长的时间才能完成。 This is why as much as possible, we use a StringBuilder to concatenate the stuff we want to print in memory . 这就是为什么我们尽可能使用StringBuilder来连接我们想要在内存中打印的东西。 After pooling everything we want to print in memory , this is the time in which you would want to call System.out.println(). 在汇总了我们想要在内存中打印的所有内容之后,这是您想要调用System.out.println()的时间。

Calling System.out.println() has no effect on memory. 调用System.out.println()对内存没有影响。

It is good to maintain logging files rather than system.out.println .If you use this only devolopers can see on the console.You can not see more messages with this. 维护日志文件而不是system.out.println是很好的。如果你使用它,只有devolopers可以在控制台上看到。你看不到更多的消息。

But you can not use it for future use.If you use logging you can save the messages into database or files ....etc 但是你不能将它用于将来使用。如果你使用日志记录,你可以将消息保存到数据库或文件....等

I believe that you are speaking about Java in this context and going to illustrate another method against system.out.println() . 我相信你在这个上下文中谈论Java,并将说明针对system.out.println()另一种方法。

System.out.print(); // Prints a string of characters only
System.out.println(); // Prints a string of character followed by a line break and that line break is the one which is taking up tons of space if I ain't wrong.

Hope this helps. 希望这可以帮助。

putting System.out.println its very worst practice. System.out.println最糟糕的做法。 try to implement logging concept. 尝试实现日志记录概念。 Use log4j OR log4j2 for mantaining the log of your system. 使用log4jlog4j2来保存系统日志。 for tracing or debugging the code. 用于跟踪或调试代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM