aspose file tools*
The moose likes Performance and the fly likes Writing to file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Writing to file" Watch "Writing to file" New topic
Author

Writing to file

Saurabh Agrawal
Ranch Hand

Joined: Oct 07, 2003
Posts: 244
Hi all,
In my Java Program , i am writing approximate 2000 lines each of 500 characters length in a text file.

These lines are written in text file as a result of 2000 iterations performed in a loop and writing individual line.

I was doing performance analysis on this and found it takes lots of time in executing my code.So what can be the possible solution which would enhance the speed of my code.

I know we can buffer the data of 2000 lines in some buffer and then dump the same in a text file.What you be the best solution to enhance the performance?

I am using OutputSteam to write the records:

Ex: OutputStream out;
out.write(object);

Any tips on performance enhancement would be appreciated.
Thanks in advance,
Saurabh


Success is not doing extraordinary things but doing ordinary things extraordinarily well.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

If you're writing text, you should be using Writers rather than OutputStreams.

Anyway, the best thing you can probably do is wrap your OutputStream (Writer) in a BufferedOutputStream (BufferedWriter). This will take care of batching the underlying OS data handoffs for you, and can speed things up quite a bit.

Of course, the problem might be your data processing, particular if you're doing a lot of String concatenation. Your profiling ought to tell you where the time is going.


[Jess in Action][AskingGoodQuestions]
Saurabh Agrawal
Ranch Hand

Joined: Oct 07, 2003
Posts: 244
Originally posted by Ernest Friedman-Hill:
If you're writing text, you should be using Writers rather than OutputStreams.

Anyway, the best thing you can probably do is wrap your OutputStream (Writer) in a BufferedOutputStream (BufferedWriter). This will take care of batching the underlying OS data handoffs for you, and can speed things up quite a bit.

Of course, the problem might be your data processing, particular if you're doing a lot of String concatenation. Your profiling ought to tell you where the time is going.


Thanks for the reply.I would like to know how fast a BufferedOutputStream be as compared to outputstream ? Can you give you some apprx stats like 1 min to 30 seconds.

Also what does BufferedOutputStream do which makes it faster?

Saurabh
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Sending data from a user program to the operating system is a slow operation, but that's what you have to do to write to a disk. If you send a tiny bit at a time, you have to do that slow operation many times. If you instead save up a bunch of data, then send it to the OS all at once, things will go faster. That's what BufferedX will do -- save up a large chunk (you can specify the size, but it's something like 2Kb by default) and send it to the OS all at once.

How much faster depends on your OS and hardware, but gains of an order of magnitude in speed are not unheard of.

Now, whether this will speed your program up at depends on whether I/O is really the bottleneck -- something else you're doing might actually be the slow part.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing to file
 
Similar Threads
When to commit
Appending Text to AutoExce.bat
Specific threads question....
NIO with large files
Inserting a BLOB into MSSQL Server 2000