是否存在缓冲的ObjectInputStream? [英] Does a Buffered ObjectInputStream exist?

查看:170
本文介绍了是否存在缓冲的ObjectInputStream?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从一个大小为350KB的文件中反序列化一个对象,并且需要相当长的时间。我的计算机科学TA告诉我,有一种方法可以使用Buffered阅读器和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.

推荐答案

您使用装饰来缓冲输入流。像这样

You use decoration to buffer the input stream. Like this

   InputStream in = ...; // your underlying stream (e.g. FileInputStream)
   ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(in));

这将确保每次调用ObjectInputStream都不会调用基本流 ,例如OS的文件读取系统调用。相反,每次调用都转到缓冲的输入流,该输入流获取并缓存数据块(默认为8K),并从中读取。这更快,因为从流中读取现在是java中的本地方法调用,并且不太经常遇到系统调用的方法调用开销。缓存一致性和JIT优化也可以在提高性能方面发挥作用。

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆