-1

I implemented a way to write primitive Java types to a binary output file using DataOutputStream's writeX methods, but I'm observing 60-fold slower performance relative to a previous implementation that writes to a text file via a BufferedWriter.

I create my output stream like so:

DataOutputStream outFile = new DataOutputStream(new FileOutputStream("fileLoc"));

I use this method to write to such streams:

public static void writeFunctionPoint (DataOutputStream outFile, FunctFileSortCriterion functPt) throws IOException
{
    outFile.writeLong   (functPt.time);
    outFile.writeBytes  (functPt.dfid);
    outFile.writeDouble (functPt.value);
    outFile.writeInt    (functPt.qualifier);

}   // end method writeFunctionPoint

Why is my new approach so much slower than my old one?

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
Sean
  • 1,283
  • 9
  • 27
  • 43
  • without seeing your code, we are guessing. Are you asking if those classes are inherently slow? – Rabbit Guy Jul 18 '16 at 14:35
  • Give or take yes, my previous implementation involved using a BufferedWriter and writing strings directly to a text file, however I've found using this new implementation to be extremely costly – Sean Jul 18 '16 at 14:36
  • Is your `DataOutputStream` buffered (by being wrapped around a `BufferedOutputStream`)? – John Bollinger Jul 18 '16 at 14:40
  • 1
    More generally, don't make us guess. Even with your edit, there is too much we don't know about your program to give a reliable answer. Present a [mcve]. – John Bollinger Jul 18 '16 at 14:42
  • DataOutputStream outFile = new DataOutputStream(new FileOutputStream("fileLoc")); No bufferedoutputstream – Sean Jul 18 '16 at 14:43
  • 2
    Please do not make people dig through the comments to find essential information. Reading the comments should never be required in order to understand what's going on. You should provide updates as an edit to the original question. – dcsohl Jul 18 '16 at 14:54

1 Answers1

4

You started out using a BufferedWriter and switched to an unbuffered OutputStream. I/O buffering can have a tremendous impact on performance, especially if you're writing a large number of small pieces. Insert a BufferedOutputStream:

DataOutputStream outFile = new DataOutputStream(
        new BufferedOutputStream(
        new FileOutputStream("fileLoc")));

That ought to get you a substantial speedup vs. where you are now. You can also try tweaking the buffer size to tune the performance a bit. I can't say how that will compare to your original BufferedWriter-based implementation, however; I'd guess it will be at least comparable, but performance is very hard to predict in general. It is essential to test.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Thank you for posting a logical and straight forward answer, and not bashing me for not following the posting rules and guidelines 100%. – Sean Jul 18 '16 at 15:13