輸出字符串到系統的最快方法是什么?

[英]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.

基本上,我計算一個整數並輸出一個字符串大約10K-100K次,然后需要將結果寫入system。輸出,每個結果用換行符分隔。

What's the fastest way to achieve this?

最快的方法是什么?

4 个解决方案

#1


23  

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

結果: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.

這些建議都比System.out.println表現得更好。BufferedOutputStream似乎是最安全的選擇,因為它在兩個測試環境中都表現良好。不過BufferedWriter也許更快。

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

如果有人有什么想法,請發表進一步的建議。我肯定有人能讓它跑得更快。

#2


4  

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.

對於大量的數據,System.out。println可能是低效的,因為它不能很好地緩沖。在這種情況下,您可以使用BufferedOutputStream或BufferedWriter。

#3


2  

Keep in mind that I/O operations are very slow compared to in-memory processing (e.g. parsing of Integer). So, I would propose you to create the whole string 'in advance' and then print it out only once (of course if its possible):

請記住,與內存中處理(例如解析整數)相比,I/O操作非常慢。所以,我建議你“提前”創建整個字符串,然后只打印一次(當然,如果可能的話):

StringBuilder sb = new StringBuilder();

新StringBuilder();

for(int i = 0 ; i < 100000; i++) { sb.append(i).append("\n");}
String printMe = sb.toString(); 
System.out.println(printMe);

There are various techniques like buffering the the level of output stream you're using, but I assume that you prefer to stay with the most basic System.out.println

有各種技術,比如緩沖正在使用的輸出流的級別,但是我假設您更喜歡使用最基本的System.out.println

Hope this helps

希望這有助於

#4


0  

The slowest part of writing to System.out is the time taken to display what you are writing. i.e. for every line you write the computer has to turn the information into pixels using a font and scroll a whole line. This is much more work than whatever you are likely to be doing to display the text.

寫系統最慢的部分。輸出是顯示你所寫內容的時間。也就是說,對於你寫的每一行,計算機必須使用字體將信息轉換成像素,並滾動一整行。這比顯示文本要做的工作要多得多。

You can speed up writing to the console by

您可以通過以下方法加快對控制台的編寫

  • writing less (usually the best idea)
  • 少寫(通常是最好的想法)
  • writing to a file instead (This can be 5-10x faster)
  • 寫入文件(這可以更快5-10x)

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/08/06/72f465d348def5dee8f14da14abe9cb3.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com