4

I want to print 1 million lines to stdout.

System.out.println(result);

Or

PrintWriter out = new PrintWriter(System.out);
out.println(result);
out.flush();

To get better performance(speed wise), which one should I use and why?

--Edit-- How about BufferedWriter?

noooooooob
  • 1,872
  • 3
  • 21
  • 27

3 Answers3

3

PrintWritter gives better performance, though the time difference is not quite visible in smaller programs. Performace Graph But becomes quite apparent as the number of lines to be printed increases. Performance Graph

I used the execution time of these snippets for the test. System.out.println(i)

class Sprint{
    public static void main(String[] args) {
        int n=10000000;
        for(int i=0;i<n;i++){
            System.out.println(i);
        }
    }
}

out.println(i);

import java.io.*;
class Pprint{
    public static void main(String[] args) {
        PrintWriter out = new PrintWriter(System.out);
        int n=10000000;
        for(int i=0;i<n;i++){
            out.println(i);
        }
        out.flush();
    }
}

I used n=10 to 10^7 and executed both of them 3 times for each n. There was a visible difference in performance after the value of n exceeds 10^3.

Abhinav Mani
  • 300
  • 1
  • 2
  • 11
0

Wrapping System.out in a different output stream won't really make a difference. It will just call the same methods. Your limitations are creating millions of small objects and the console's ability to receive, hold and display everything.

Also, designing a simple test is easy.

public static void main(String[] args) {
    System.out.println();
    long start = 0L;

    start = System.currentTimeMillis();

    for(int i = 0; i <= 999999; i++)
        System.out.println(i);

    long printStreamTime = System.currentTimeMillis() - start;

    PrintWriter writer = new PrintWriter(System.out);

    System.gc();
    try {
        Thread.sleep(1000L);
    } catch(InterruptedException ie) {}

    start = System.currentTimeMillis();

    for(int i = 0; i <= 999999; i++)
        writer.println(i);

    long printWriterTime = System.currentTimeMillis() - start;

    System.out.println();

    System.out.println("PrintStream time = " + (printStreamTime / 1000.0));
    System.out.println("PrintWriter time = " + (printWriterTime / 1000.0));
}

I got ~49 seconds for both. Almost identical.

If you want speed, write to a file and open it.

Radiodef
  • 37,180
  • 14
  • 90
  • 125
  • Thanks for the answer. Will the 'auto flush' boolean make any difference ? http://docs.oracle.com/javase/6/docs/api/java/io/PrintWriter.html#PrintWriter%28java.io.OutputStream,%20boolean%29 – noooooooob Jan 25 '14 at 06:25
0

My suggestion is to use PrintWriter for better time performance, though the time difference is not major in smaller programs. For larger programs you may notice significant difference. I have replaced and run System.out.println with PrintWriter and my execution time reduced from 0.06 sec to 0.05 sec.

Here is a link to a similar question: https://discuss.codechef.com/questions/62586/systemoutprintln-vs-printwriter

Hope it helps :)

CuriousCurie
  • 113
  • 10
  • 1
    Dear fellow stackers, if in any case you downvote any answer, it's your duty to communicate what went wrong. Its just plain wrong to not give any reason; the other person doesn't know what's wrong and will keep on doing it unless you point it out. – CuriousCurie Oct 07 '18 at 15:35