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

  • SEARCH
  • Home
  • 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 7965823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:15:30+00:00 2026-06-04T06:15:30+00:00

Possible Duplicate: How do you determine the ideal buffer size when using FileInputStream? When

  • 0

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

When reading raw data from a file (or any input stream) using either the C++’s istream family’s read() or C’s fread(), a buffer has to be supplied, and a number of how much data to read. Most programs I have seen seem to arbitrarily chose a power of 2 between 512 and 4096.

  1. Is there a reason it has to/should be a power of 2, or this just programer’s natural inclination to powers of 2?
  2. What would be the “ideal” number? By “ideal” I mean that it would be the fastest. I assume it would have to be a multiple of the underlying device’s buffer size? Or maybe of the underlying stream object’s buffer? How would I determine what the size of those buffers is, anyway? And once I do, would using a multiple of it give any speed increase over just using the exact size?

EDIT
Most answers seem to be that it can’t be determined at compile time. I am fine with finding it at runtime.

  • 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-04T06:15:31+00:00Added an answer on June 4, 2026 at 6:15 am

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

    Optimum buffer size is related to a number of things: file system
    block size, CPU cache size and cache latency.

    Most file systems are configured to use block sizes of 4096 or 8192.
    In theory, if you configure your buffer size so you are reading a few
    bytes more than the disk block, the operations with the file system
    can be extremely inefficient (i.e. if you configured your buffer to
    read 4100 bytes at a time, each read would require 2 block reads by
    the file system). If the blocks are already in cache, then you wind up
    paying the price of RAM -> L3/L2 cache latency. If you are unlucky and
    the blocks are not in cache yet, the you pay the price of the
    disk->RAM latency as well.

    This is why you see most buffers sized as a power of 2, and generally
    larger than (or equal to) the disk block size. This means that one of
    your stream reads could result in multiple disk block reads – but
    those reads will always use a full block – no wasted reads.

    Ensuring this also typically results in other performance friendly parameters affecting both reading and subsequent processing: data bus width alignment, DMA alignment, memory cache line alignment, whole number of virtual memory pages.

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

Sidebar

Related Questions

Possible Duplicate: Is there any way to determine text direction from CultureInfo in asp.net?
Possible Duplicate: How to determine if a linked list has a cycle using only
Possible Duplicate: How to determine the content size of a UIWebView? Hai Guys, I
Possible Duplicate: Best way to determine if two path reference to same file in
Possible Duplicate: In Java, what is the best way to determine the size of
Possible Duplicate: How to determine the attached type from within a custom attribute? I
Possible Duplicate: How can I determine if a python script is executed from crontab?
Possible Duplicate: Caller function in PHP 5? php: determine where function was called from
Possible Duplicate: Determine framework (CLR) version of assembly I have a library/DLL file which
Possible Duplicate: Best way to determine if two path reference to same file in

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.