• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing to file

 
Saurabh Agrawal
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Saurabh Agrawal
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic