Buffered ObjectInputStream 是否存在?
我正在从一个大小为 350KB 的文件中反序列化一个对象,并且需要相当长的时间。我的计算机科学助教告诉我,有一种方法可以使用 Buffered reader 和 ObjectInputStream 来大大提高性能。然而我在谷歌上找不到任何关于此的信息。
I am deserializing an object from a file that is 350KB in size, and its taking rather a long time. My computer science TA told me that there is a way to use a Buffered reader along with the ObjectInputStream to greatly increase performance. I however can not find anything about this on Google.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用装饰来缓冲输入流。这样
就可以保证每次调用ObjectInputStream时都不会调用基流
in
,比如OS的文件读取系统调用。相反,每个调用都会转到缓冲输入流,该输入流获取并缓存数据块(默认情况下为 8K),并从中读取。这更快,因为从流中读取现在是 java 中的本地方法调用,并且很少会遇到系统调用的方法调用开销。缓存一致性和 JIT 优化也在提高性能方面发挥着作用。You use decoration to buffer the input stream. Like this
This will ensure that each call to ObjectInputStream does not call the base stream
in
, such as the OS's file read system call. Instead, each call goes to the buffered input stream, which fetches and caches blocks of data (8K by default), and reads from that. This is faster, since reading from the stream is now a local method call in java, and the method call overhead of a system call is encountered less often. Cache coherence and JIT optimizations also come into play in improving performance.不,但你可以使用
ObjectInputStream(InputStream in) 构造函数
通过将 BufferedInputStream 作为参数传递给上述构造函数来创建缓冲对象输入流。
以下是从文件读取序列化对象的示例:
签出以下链接:
http://java.sun.com/docs/books/performance/1st_edition/html/JPIOPerformance.fm.html
No but You can use
ObjectInputStream(InputStream in) constructor
To create buffered object intput stream by passing BufferedInputStream as argument to above constructor.
Here is example for reading serialized objects from file:
Checkout following link :
http://java.sun.com/docs/books/performance/1st_edition/html/JPIOPerformance.fm.html