I have a pretty basic doubt. Often, I have to write apps which use buffered file I/O and every time I am faced with the dilemma of choosing the buffer size and I end up doing trial and error often with pretty nasty results. I want to know if there is any method or algorithm which can automatically determine the optimum buffer size for the job based on the underlying platform like Teracopy does when handling files in Windows. I mainly use Qt for the GUI.
If possible a tiny example in C/C++/C#/Java is very much appreciated!
Thanks!
In Java the optimal is usually around the L1 cache size which is typically 32 KB. In Java, at least choosing 1024 bytes or 1 MB doesn’t make much difference (<20%)
If you are reading data sequentially, usually your OS is smart enough to detect this and prefetch the data for you.
What you can do is the following. This test appears to show a significant difference in the block sizes used.
prints
What this test doesn’t show is that the hard drive only supports 60 MB/s reads and 40 MB/s writes. All you are testing is the speed in and out of cache. If this was your only priority, you would use a memory mapped file.
prints
This is so fast because it just maps the data in the OS disk cache directly into the memory of the application (so no copying is required)