Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the fastest way to output a string to system out?

Tags:

I'm doing something like this:

for (int i = 0; i < 100000; i++) {    System.out.println( i ); } 

Basically, I compute an integer and output a string about 10K-100K times and then need to write the result to system.out, each result separated by a newline.

What's the fastest way to achieve this?

like image 250
CodeSmith Avatar asked Aug 06 '12 05:08

CodeSmith


People also ask

Is PrintWriter faster than system out?

PrintWriter class is the implementation of Writer class. By using PrintWriter than using System. out. println is preferred when we have to print a lot of items as PrintWriter is faster than the other to print data to the console.

How do I save console output to string Java?

If you create a PrintStream connected to a ByteArrayOutputStream , then you can capture the output as a String . Example: // Create a stream to hold the output ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos); // IMPORTANT: Save the old System. out!

Does system out Println convert to string?

No, System. out. println() does not call toString(). It calls String.


2 Answers

Thank you for the suggestions. I created a test program to compare them:

import java.io.BufferedOutputStream; import java.io.BufferedWriter; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.lang.StringBuilder;  public class systemouttest {      public static void main(String[] args) throws Exception {          long starttime = System.currentTimeMillis();         for (int i = 0; i < 100000; i++) {            System.out.println( i );         }         long printlntime = System.currentTimeMillis();          StringBuilder sb = new StringBuilder();         for (int i = 0; i < 100000; i++) {             sb.append( i + "\n" );         }         System.out.print(sb.toString());         long stringbuildertime = System.currentTimeMillis();          OutputStream out = new BufferedOutputStream ( System.out );         for (int i = 0; i < 100000; i++) {             out.write((i + "\n").getBytes());         }         out.flush();         long bufferedoutputtime = System.currentTimeMillis();          BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));         for (int i = 0; i < 100000; i++) {             log.write(i + "\n");         }         log.flush();         long bufferedwritertime = System.currentTimeMillis();          System.out.println( "System.out.println: " + (printlntime - starttime) );         System.out.println( "StringBuilder: " + (stringbuildertime - printlntime) );         System.out.println( "BufferedoutputStream: " + (bufferedoutputtime - stringbuildertime) );         System.out.println( "BufferedWriter: " + (bufferedwritertime - bufferedoutputtime) );     }  } 

Results:

Environment1
System.out.println: 482
StringBuilder: 210
BufferedoutputStream: 86
BufferedWriter: 202

Environment2
System.out.println: 1763
StringBuilder: 45
BufferedoutputStream: 76
BufferedWriter: 34

The suggestions all performed better than System.out.println. BufferedOutputStream seems to be the safest choice as it performed well in both test environments. BufferedWriter maybe faster though.

Please post further suggestions if anyone has some ideas. I'm sure someone can make it go faster :)

like image 85
CodeSmith Avatar answered Oct 27 '22 08:10

CodeSmith


For large amount of data,System.out.println might be inefficient as it does not do very good buffering. In that case, you can use a BufferedOutputStream or a BufferedWriter.

like image 26
Akash KC Avatar answered Oct 27 '22 06:10

Akash KC