Psst.. new poll here.
[email protected] web/email now available. Want one? Go here.
Cannot use outlook/hotmail/live here to register as they blocking our mail servers. #microsoftdeez
Obey the Epel!
Paste
Pasted as Java by Jabba ( 15 years ago )
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
Revise this Paste