noooooooob
noooooooob

Reputation: 1890

System.out.println vs PrintWriter performance comparison?

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?

Upvotes: 4

Views: 2593

Answers (3)

Abhinav Mani
Abhinav Mani

Reputation: 300

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.

Upvotes: 3

CuriousCurie
CuriousCurie

Reputation: 113

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 :)

Upvotes: 0

Radiodef
Radiodef

Reputation: 37845

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.

Upvotes: 0

Related Questions