Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 + "
" );
        }
        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 + "
").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 + "
");
        }
        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 :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...