BufferedInputStream的用法 [英] Usage of BufferedInputStream

查看:187
本文介绍了BufferedInputStream的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我以一个小心谨慎的方式为这篇文章做准备。在Java方面,我是一个初学者。我已经开启和关闭了一段时间的PHP编程,但我已经准备好制作一个桌面应用程序了,所以我决定使用Java,原因有很多。

Let me preface this post with a single caution. I am a total beginner when it comes to Java. I have been programming PHP on and off for a while, but I was ready to make a desktop application, so I decided to go with Java for various reasons.

我正在处理的应用程序处于开始阶段(少于5个类),我需要从本地文件中读取字节。通常,文件目前小于512kB(但将来可能会变大)。目前,我使用 FileInputStream 将文件读入三个字节数组,完全满足我的要求。但是,我看到提到了 BufferedInputStream ,并且想知道我目前这样做的方式是最好的,还是我应该使用 BufferedInputStream

The application I am working on is in the beginning stages (less than 5 classes) and I need to read bytes from a local file. Typically, the files are currently less than 512kB (but may get larger in the future). Currently, I am using a FileInputStream to read the file into three byte arrays, which perfectly satisfies my requirements. However, I have seen a BufferedInputStream mentioned, and was wondering if the way I am currently doing this is best, or if I should use a BufferedInputStream as well.

我做了一些研究,并在Stack Overflow上阅读了一些问题,但我仍然遇到麻烦,了解何时使用的最佳情况而不是使用的BufferedInputStream 。在我的情况下,我读取字节的第一个数组只有几个字节(小于20)。如果我收到的数据在这些字节中是好的,那么我将文件的其余部分读入另外两个不同大小的字节数组。

I have done some research and have read a few questions here on Stack Overflow, but I am still having troubles understanding the best situation for when to use and not use the BufferedInputStream. In my situation, the first array I read bytes into is only a few bytes (less than 20). If the data I receive is good in these bytes, then I read the rest of the file into two more byte arrays of varying size.

我也听过很多人提到在每个特定情况下查看哪个更有效,但是,我没有剖析经验,我不确定从哪里开始。我也很喜欢这方面的一些建议。

I have also heard many people mention profiling to see which is more efficient in each specific case, however, I have no profiling experience and I'm not really sure where to start. I would love some suggestions on this as well.

我很抱歉这么长的帖子,但我真的想学习并理解做这些事情的最佳方法。我总是习惯于第二次猜测我的决定,所以我会喜欢一些反馈。谢谢!

I'm sorry for such a long post, but I really want to learn and understand the best way to do these things. I always have a bad habit of second guessing my decisions, so I would love some feedback. Thanks!

推荐答案

如果你一直在做小读,那么 BufferedInputStream 会给你带来明显更好的表现。无缓冲流上的每个读取请求通常导致对操作系统的系统调用以读取所请求的字节数。进行系统调用的开销可能是每个系统调用数千个机器指令。缓冲流通过对内部缓冲区执行(例如)高达8k字节的大量读取,然后从该缓冲区分配字节来减少此操作。这可以大大减少系统调用的数量。

If you are consistently doing small reads then a BufferedInputStream will give you significantly better performance. Each read request on an unbuffered stream typically results in a system call to the operating system to read the requested number of bytes. The overhead of doing a system call is may be thousands of machine instructions per syscall. A buffered stream reduces this by doing one large read for (say) up to 8k bytes into an internal buffer, and then handing out bytes from that buffer. This can drastically reduce the number of system calls.

但是,如果你一直在做大量读取(例如8k或更多),那么 BufferedInputStream 会减慢速度。您通常不会减少系统调用次数,缓冲会引入额外的数据复制步骤。

However, if you are consistently doing large reads (e.g. 8k or more) then a BufferedInputStream slows things. You typically don't reduce the number of syscalls, and the buffering introduces an extra data copying step.

在您的用例中(首先读取20个字节的块)那么很多大块)我会说使用 BufferedInputStream 更有可能降低性能而不是增加性能。但最终,它取决于实际的阅读模式。

In your use-case (where you read a 20 byte chunk first then lots of large chunks) I'd say that using a BufferedInputStream is more likely to reduce performance than increase it. But ultimately, it depends on the actual read patterns.

这篇关于BufferedInputStream的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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