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

后端 未结 9 1761
北海茫月
北海茫月 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:45

    You could use the BufferedStreams/readers and then use their buffer sizes.

    I believe the BufferedXStreams are using 8192 as the buffer size, but like Ovidiu said, you should probably run a test on a whole bunch of options. Its really going to depend on the filesystem and disk configurations as to what the best sizes are.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 17:54

    Reading files using Java NIO's FileChannel and MappedByteBuffer will most likely result in a solution that will be much faster than any solution involving FileInputStream. Basically, memory-map large files, and use direct buffers for small ones.

    0 讨论(0)
提交回复
热议问题