user1696229
user1696229

Reputation: 41

anyway to speed up printwriter?

I am having over 20s 5000x30 double arrays and each of which will be written to text files using:

PrintWriter test = new PrintWriter(new BufferedWriter(new FileWriter("test.txt")));

The processing took me over 10 minutes and I'd like to see if there is any alternative way to speed up the process.

Upvotes: 4

Views: 3811

Answers (3)

Munesh
Munesh

Reputation: 1569

Set the autoFlush variable of the PrintWriter to false.

PrintWriter test = new PrintWriter(new BufferedWriter(
                                   new FileWriter("test.txt")), **false**);

Once entire writing ( println ) is done, Call test.flush()

This way you can avoid intermediate flushing time.

Upvotes: 0

Santosh
Santosh

Reputation: 17893

I would recommend using java nio for this. They are inherently faster than traditional java io. Please refer to these (1,2) examples to start with.

Upvotes: 0

daniel gratzer
daniel gratzer

Reputation: 53871

Instead of dealing with everything over numerous buffer, avoid flushing the buffer until absolutely necessary.

In practice this means, don't use println printf flush format or any other method that would flush the buffer. By avoiding this you delay and combine costly system calls that eat up your runtime.

Alternatively set autoFlush to false in the constructor for PrintWriter. Check out this question for some more info

Upvotes: 3

Related Questions