Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8217035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:20:31+00:00 2026-06-07T12:20:31+00:00

I have a pretty basic doubt. Often, I have to write apps which use

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T12:20:32+00:00Added an answer on June 7, 2026 at 12:20 pm

    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.

    public static void main(String... args) throws IOException {
        for (int i = 512; i <= 2 * 1024 * 1024; i *= 2)
            readWrite(i);
    }
    
    private static void readWrite(int blockSize) throws IOException {
        ByteBuffer bb = ByteBuffer.allocateDirect(blockSize);
        long start = System.nanoTime();
        FileChannel out = new FileOutputStream("deleteme.dat").getChannel();
        for (int i = 0; i < (1024 << 20); i += blockSize) {
            bb.clear();
            while (bb.remaining() > 0)
                if (out.write(bb) < 1) throw new AssertionError();
        }
        out.close();
        long mid = System.nanoTime();
        FileChannel in = new FileInputStream("deleteme.dat").getChannel();
        for (int i = 0; i < (1024 << 20); i += blockSize) {
            bb.clear();
            while (bb.remaining() > 0)
                if (in.read(bb) < 1) throw new AssertionError();
        }
        in.close();
        long end = System.nanoTime();
        System.out.printf("With %.1f KB block size write speed %.1f MB/s, read speed %.1f MB/s%n",
                blockSize / 1024.0, 1024 * 1e9 / (mid - start), 1024 * 1e9 / (end - mid));
    }
    

    prints

    With 0.5 KB block size write speed 96.6 MB/s, read speed 169.7 MB/s
    With 1.0 KB block size write speed 154.2 MB/s, read speed 312.2 MB/s
    With 2.0 KB block size write speed 201.5 MB/s, read speed 438.7 MB/s
    With 4.0 KB block size write speed 288.0 MB/s, read speed 733.9 MB/s
    With 8.0 KB block size write speed 318.4 MB/s, read speed 711.8 MB/s
    With 16.0 KB block size write speed 540.6 MB/s, read speed 1263.7 MB/s
    With 32.0 KB block size write speed 726.0 MB/s, read speed 1370.9 MB/s
    With 64.0 KB block size write speed 801.8 MB/s, read speed 1536.5 MB/s
    With 128.0 KB block size write speed 857.5 MB/s, read speed 1539.6 MB/s
    With 256.0 KB block size write speed 794.0 MB/s, read speed 1781.0 MB/s
    With 512.0 KB block size write speed 676.2 MB/s, read speed 1221.4 MB/s
    With 1024.0 KB block size write speed 886.3 MB/s, read speed 1501.5 MB/s
    With 2048.0 KB block size write speed 784.7 MB/s, read speed 1544.9 MB/s
    

    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.

    int blockSize = 32 * 1024;
    ByteBuffer bb = ByteBuffer.allocateDirect(blockSize);
    FileChannel out = new FileOutputStream("deleteme.dat").getChannel();
    for (int i = 0; i < (1024 << 20); i += blockSize) {
        bb.clear();
        while (bb.remaining() > 0)
            if (out.write(bb) < 1) throw new AssertionError();
    }
    out.close();
    
    long start = System.nanoTime();
    FileChannel in = new FileInputStream("deleteme.dat").getChannel();
    MappedByteBuffer map = in.map(FileChannel.MapMode.READ_ONLY, 0, in.size());
    in.close();
    long end = System.nanoTime();
    System.out.printf("Mapped file at a rate of %.1f MB/s%n",
            1024 * 1e9 / (end - start));
    

    prints

    Mapped file at a rate of 589885.5 MB/s
    

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pretty basic doubt on dirty read/writes to a value stored in
I might have pretty basic question about regex. I have the following regex, which
I have a very basic doubt regarding cloud computing that is catching up pretty
I have a script which I think is pretty basic scraping, call it what
Pretty Basic one here guys. I have a View which holds 2 textfields for
I have a pretty basic question: In GAE, if I use memcache to store
I have a pretty basic java applet that allows users to use a signature
I have a pretty basic jquery script which uses an h2 as a trigger
I have a pretty basic positional inverted index, in which I store a lot
I have a pretty basic page that shows every five minutes of the day

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.