HOME - Recent Changes - Search:

Academic Work


Personal

* pot de départ


dblp


(:twitter:)

-----

[ edit | logout ]
[ help | sandbox | passwd ]

Accelerate System.out.print

#############################

Problem

Using System.out.print to produce a large output can be very expensive. Fortunately, there is a very efficient solution for this.

Solution

Let's see a comparative test first:

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;

/**
 * how to accelerate System.out.print()
 * 
 * @author jabba
 */
public class TurboPrint
{
   private final static int MAX = 1000000;   // 1_000_000

   private final static int BUFFER = 4096;   // 4 KB

   private final static PrintStream bufferedSystemOut = 
      new PrintStream(new BufferedOutputStream(System.out, BUFFER));

   public static void main(String[] args) throws Exception
   {
      TurboPrint main = new TurboPrint();
      //main.test_01();   // 10697 msec.
      //main.test_02();   // 392 msec.
      //main.test_03();   // 1514 msec.
      main.test_04();   // 382 msec., WINNER
   }

   /**
    * Naive approach.
    */
   private void test_01()
   {
      System.err.println("Test 01");
      System.err.println("=======");
      //
      long start = System.currentTimeMillis();

      for (int i = 0; i < MAX; ++i) 
      {
         System.out.print("abcdefghijk ");
         System.out.print(String.valueOf(i));
         System.out.print('\n');
      }

      System.err.println("Loop time: " + (System.currentTimeMillis() - start));
   }

   private void test_02() throws Exception
   {
      System.err.println("Test 02");
      System.err.println("=======");
      //
      BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new
            FileOutputStream(java.io.FileDescriptor.out), "ASCII"), BUFFER);

      long start = System.currentTimeMillis();

      for (int i = 0; i < MAX; i++) 
      {
          out.write("abcdefghijk ");
          out.write(String.valueOf(i));
          out.write('\n');
      }
      out.flush();

      System.err.println("Loop time: " + (System.currentTimeMillis() - start));
   }

   private void test_03()
   {
      System.err.println("Test 03");
      System.err.println("=======");
      //
      long start = System.currentTimeMillis();

      for (int i = 0; i < MAX; ++i) 
      {
         bufferedSystemOut.print("abcdefghijk ");
         bufferedSystemOut.print(String.valueOf(i));
         bufferedSystemOut.print('\n');
      }
      bufferedSystemOut.flush();

      System.err.println("Loop time: " + (System.currentTimeMillis() - start));
   }

   private void test_04() throws Exception
   {
      System.err.println("Test 04");
      System.err.println("=======");
      //
      BufferedWriter out = 
         new BufferedWriter(new OutputStreamWriter(System.out), BUFFER);

      long start = System.currentTimeMillis();

      for (int i = 0; i < MAX; i++) 
      {
          out.write("abcdefghijk ");
          out.write(String.valueOf(i));
          out.write('\n');
      }
      out.flush();

      System.err.println("Loop time: " + (System.currentTimeMillis() - start));
   }

} // eof class TurboPrint

That is, use the following code:

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out), 4096);

out.write("abcdefghijk ");
...
out.flush();   // DON'T FORGET THIS

See also http://www.rgagnon.com/javadetails/java-0603.html .

Cloud City


anime | bash | blogs | bsd | c/c++ | c64 | calc | comics | convert | cube | del.icio.us | digg | east | eBooks | egeszseg | elite | firefox | flash | fun | games | gimp | google | groovy | hardware | hit&run | howto | java | javascript | knife | lang | latex | liferay | linux | lovecraft | magyar | maths | movies | music | p2p | perl | pdf | photoshop | php | pmwiki | prog | python | radio | recept | rts | scala | scene | sci-fi | scripting | security | shell | space | súlyos | telephone | torrente | translate | ubuntu | vim | wallpapers | webutils | wikis | windows


Blogs and Dev.

* Ubuntu Incident
* Python Adventures
* me @ GitHub


Places

Debrecen | France | Hungary | Montreal | Nancy


Notes

full circle | km


Hobby Projects

* Jabba's Codes
* PmWiki
* Firefox
* PHP
* JavaScript
* Scriptorium
* Tutorials
* me @ GitHub


Quick Links


[ edit ]

View - Edit - History - Attach - Print *** Report - Recent Changes - Search
Page last modified on 2009 November 27, 23:30