How do you determine the ideal buffer size when using FileInputStream?

后端 未结 9 1751
北海茫月
北海茫月 2020-11-22 17:01

I have a method that creates a MessageDigest (a hash) from a file, and I need to do this to a lot of files (>= 100,000). How big should I make the buffer used to read from t

9条回答
  •  北海茫月
    2020-11-22 17:48

    1024 is appropriate for a wide variety of circumstances, although in practice you may see better performance with a larger or smaller buffer size.

    This would depend on a number of factors including file system block size and CPU hardware.

    It is also common to choose a power of 2 for the buffer size, since most underlying hardware is structured with fle block and cache sizes that are a power of 2. The Buffered classes allow you to specify the buffer size in the constructor. If none is provided, they use a default value, which is a power of 2 in most JVMs.

    Regardless of which buffer size you choose, the biggest performance increase you will see is moving from nonbuffered to buffered file access. Adjusting the buffer size may improve performance slightly, but unless you are using an extremely small or extremely large buffer size, it is unlikely to have a signifcant impact.

提交回复
热议问题