Java Effect - System.out on Performance

I saw this question , and it is somewhat similar. I would like to know if this is really a big factor that will affect the performance of my application. Here is my script.

I have a Java Webapp that can load thousands of data from a table that reads in a row from top to bottom. I use System.out.println() to show on the server side which line the application is currently reading.

- I know about creating a log file. In fact, I am creating a log file and at the same time displaying the logs on the server command line.
Is there any other way to print the current data in the prompt?

+2
java performance performance-testing outputstream
source share
5 answers

This may affect the performance of your application. The value will vary depending on the type of equipment used and the load on the host.

Some points on which this can be translated into performance:

-> As stated by Rocket boy, println is synchronized, which means that you will encounter blocking overhead on the object header and may cause bottlenecks in the stream depending on your design.

-> To print to the console, kernel time is required, kernel time means that the processor will not work in user mode, which basically means that your processor will be busy executing kernel code instead of application code.

-> If you already register this, this means extra kernel time for I / O, and if your platform does not support asynchronous I / O, this means that your processor may stall while it is busy.

You can try and compare this and see for yourself.

There are ways to get rid of this, for example, with very fast I / O, a huge machine for special use, and possibly an offset lock on your JVM settings if your application design is not multithreaded for this console.

Like everything in performance, it all depends on your equipment and priorities.

+7
source share

I recently tested (read and write) large (1-1.5gb) text files, and I found out that:

 PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(java.io.FileDescriptor.out), "UTF-8"), 512)); out.println(yourString); //... out.flush(); 

actually almost 250% faster than

 System.out.println(yourString); 

My test program first read about 1 gigabyte of data, processed it a bit, and output it in a slightly different format.

Test results (on a Macbook Pro, with SSD reading and burning using the same disc):

  • data-output-to-system-out> output.txt => 1min32sec
  • data-written-to-file-in-java => 37sec
  • data-written-to-buffered-writer-stdout> output.txt => 36sec

I tried with several buffers ranging in size from 256 to 10k, but that didn't matter.

So keep in mind if you are creating unix command-line tools with Java, where the output should be directed or redirected to another place, do not use System.out directly!

+7
source share
 System.out.println() 

synchronized.

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

If several streams are written to it, performance will suffer.

+5
source share

Yes, it will be a HUGE performance impact. If you want to quantify the quantity, then there are a lot of software and / or ways to measure your own code performance.

+3
source share

System.out.println is very slow compared to most slow operations. This is because it works more on the machine than other I / O operations (and single-threaded)

I suggest you write the output to a file and tail output of this file. Thus, the output will be slow, but it will not slow down your web service.

+2
source share

All Articles