[英]Java - DataOutputStream writeLong\Byte\Double\Int Speed
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
. 我实现了一个办法写原始的Java类型使用二进制输出文件
DataOutputStream
的writeX方法,但我观察相对较慢的性能到以前实现60倍于写入通过一个文本文件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? 为什么我的新方法比以前的方法慢得多?
You started out using a BufferedWriter
and switched to an unbuffered OutputStream
. 您开始使用
BufferedWriter
并切换到无缓冲的OutputStream
。 I/O buffering can have a tremendous impact on performance, especially if you're writing a large number of small pieces. I / O缓冲会对性能产生巨大影响,尤其是在编写大量小片段的情况下。 Insert a
BufferedOutputStream
: 插入一个
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; 但是,我不能说这将与您最初的基于
BufferedWriter
的实现相比。 I'd guess it will be at least comparable, but performance is very hard to predict in general. 我想它至少可以与之媲美,但总体上很难预测性能。 It is essential to test.
测试至关重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.