I've seen this question and it's somewhat similar. I would like to know if it really is a big factor that would affect the performance of my application. Here's my scenario.
I have this Java webapp that can upload thousands of data from a Spreadsheet which is being read per row from top to bottom. I'm using System.out.println()
to show on the server's side on what line the application is currently reading.
- I'm aware of creating a log file. In fact, I'm creating a log file and at the same time, displaying the logs on the server's prompt.
Is there any other way of printing the current data on the prompt?

- 1
- 1

- 4,213
- 9
- 37
- 60
-
1System.out is slow, but whether it impacts the overal perf of your application depends entirely on what it does. This question can't be answered as is. – ewernli Sep 03 '13 at 05:40
-
You could fire off the printing in another thread. Of course, then you wouldn't be up to date on exactly where your code is, at least it wouldn't slow you down. Beware of how big buffer you have though. – jontejj Sep 03 '13 at 05:44
-
@ Michael Ardan : if the server process thousands of record and print it on the console, do you really see them? It it for debugging? – Jayan Sep 03 '13 at 05:47
-
@Jayan : Yes, it can be seen from the console. – Michael 'Maik' Ardan Sep 03 '13 at 05:47
-
It also depends on the ratio of println calls to other functions your code is executing. Objectively, it doesn't, but if your program takes 60 secods to run and excess printlns account for 2 seconds, it seems less relevant. – Cole Henrich May 22 '21 at 01:37
6 Answers
I was recently testing with (reading and) writing 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();
is in fact almost 250% faster than
System.out.println(yourString);
My test-program first read about 1gb of data, processed it a bit and outputted it in slightly different format.
Test results (on Macbook Pro, with SSD reading&writing using same disk):
- 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 did try with multiple buffer sized between 256-10k but that didn't seem to matter.
So keep in mind if you're creating unix command-line tools with Java where output is meant to be directed or piped to somewhere else, don't use System.out directly!

- 2,199
- 2
- 16
- 14
-
Thank you for the performance stats! How does using a BufferedWriter compare in performance to using a Logger object? – Aug 04 '15 at 09:55
-
now this is a buffered writer, how does performance compare when the buffer is full? – tuskiomi Jun 26 '17 at 18:23
-
Greetings from the future! For comparison, a 2018 GNU/Linux glibc stdout buffer is 4096 bytes while a file copy buffer is 131072 bytes. The copying tool `dd` is stuck with a default 512 byte buffer (like this answer) due to spec, which makes it CPU bound on modern drives. – that other guy Jun 09 '18 at 20:48
It can have an impact on your application performance. The magnitude will vary depending on the kind of hardware you are running on and the load on the host.
Some points on which this can translate to performance wise:
-> Like Rocket boy stated, println is synchronized, which means you will be incurring in locking overhead on the object header and may cause thread bottlenecks depending on your design.
-> Printing on the console requires kernel time, kernel time means the cpu will not be running on user mode which basically means your cpu will be busy executing on kernel code instead of your application code.
-> If you are already logging this, that means extra kernel time for I/O, and if your platform does not support asynchronous I/O this means your cpu might become stalled on busy waits.
You can actually try and benchmark this and verify this yourself.
There are ways to getaway with this like for example having a really fast I/O, a huge machine for dedicated use maybe and biased locking on your JVM options if your application design will not be multithreaded on that console printing.
Like everything on performance, it all depends on your hardware and priorities.

- 621
- 3
- 8
-
If I'm integration testing with a tool such as Selenium's WebDriver, how much of a performance hit will using System.out.println have on integration testing small test applications? – Aug 04 '15 at 09:53
System.out.println()
is synchronized.
public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
If multiple threads write to it, its performance will suffer.

- 9,573
- 2
- 34
- 36
Yes, it will have a HUGE impact on performance. If you want a quantifiable number, well then there's plenty of software and/or ways of measuring your own code's performance.

- 1,339
- 3
- 16
- 22
System.out.println is very slow compared to most slow operations. This is because it places more work on the machine than other IO operations (and it is single threaded)
I suggest you write the output to a file and tail
the output of this file. This way, the output will still be slow, but it won't slow down your web service so much.

- 525,659
- 79
- 751
- 1,130
Here's a very simple program to check performance of System.out.println and compare it with multiplication operation (You can use other operations or function specific to your requirements).
public class Main{
public static void main(String []args) throws InterruptedException{
long tTime = System.nanoTime();
long a = 123L;
long b = 234L;
long c = a*b;
long uTime = System.nanoTime();
System.out.println("a * b = "+ c +". Time taken for multiplication = "+ (uTime - tTime) + " nano Seconds");
long vTime = System.nanoTime();
System.out.println("Time taken to execute Print statement : "+ (vTime - uTime) + " nano Seconds");
}
}
Output depends on your machine and it's current state.
Here's what I got on : https://www.onlinegdb.com/online_java_compiler
a * b = 28782. Time taken for multiplication = 330 nano Seconds
Time taken to execute Print statement : 338650 nano Seconds
EDIT :
I have logger set up on my local machine so wanted to give you idea of performance difference between system.out.println and logger.info - i.e., performance comparison between console print vs logging
public static void main(String []args) throws InterruptedException{
long tTime = System.nanoTime();
long a = 123L;
long b = 234L;
long c = a*b;
long uTime = System.nanoTime();
System.out.println("a * b = "+ c +". Time taken for multiplication = "+ (uTime - tTime) + " nano Seconds");
long vTime = System.nanoTime();
System.out.println("Time taken to execute Print statement : "+ (vTime - uTime) + " nano Seconds");
long wTime = System.nanoTime();
logger.info("a * b = "+ c +". Time taken for multiplication = "+ (uTime - tTime) + " nano Seconds");
long xTime = System.nanoTime();
System.out.println("Time taken to execute log statement : "+ (xTime - wTime) + " nano Seconds");
}
Here's what I got on my local machine :
a * b = 28782. Time taken for multiplication = 1667 nano Seconds
Time taken to execute Print statement : 34080917 nano Seconds
2022-11-15 11:36:32.734 [] INFO CreditAcSvcImpl uuid: - a * b = 28782. Time taken for multiplication = 1667 nano Seconds
Time taken to execute log statement : 9645083 nano Seconds
Notice that system.out.println is taking almost 24 ms higher then the logger.info.

- 1,230
- 17
- 22