Why do BufferedInputStream improve performance?

Discussion in 'Java' started by Marc Twain, Jan 15, 2004.

  1. Marc Twain

    Marc Twain Guest

    I'm puzzled. Some books recommend the use of BufferedInputStream when
    reading from a file, but they do not explain why. How could a buffer,
    which is essentially a cache, improve performance? After all a file
    can only be fully read once the last byte of data was read from disk,
    which will happen at the same time whether you buffer the input or
    not.

    Obviously I must be wrong, could someone explain why?

    Thanks,

    AJ.
     
    Marc Twain, Jan 15, 2004
    #1
    1. Advertising

  2. On 15 Jan 2004 03:30:22 -0800, Marc Twain wrote:
    > I'm puzzled. Some books recommend the use of BufferedInputStream when
    > reading from a file, but they do not explain why. How could a buffer,
    > which is essentially a cache, improve performance? After all a file
    > can only be fully read once the last byte of data was read from disk,
    > which will happen at the same time whether you buffer the input or
    > not.
    >
    > Obviously I must be wrong, could someone explain why?


    Accessing the physical file on the disk takes time that is related to
    other things than just the amount of data you transfer, for example
    crossing the user/system boundary, and moving the disk heads into
    position. Basically if you can reduce the number of *times* you need
    to access the file, you can reduce the overall time needed to read it.

    So the BufferedInputStream initially reads more data than you've
    requested (probably some multiple of a typical disk block size),
    allowing your application to read small amounts of data from the BIS
    far more cheaply than if it actually read from the file each time.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Jan 15, 2004
    #2
    1. Advertising

  3. Marc Twain

    Jon Skeet Guest

    Marc Twain <> wrote:
    > I'm puzzled. Some books recommend the use of BufferedInputStream when
    > reading from a file, but they do not explain why. How could a buffer,
    > which is essentially a cache, improve performance? After all a file
    > can only be fully read once the last byte of data was read from disk,
    > which will happen at the same time whether you buffer the input or
    > not.
    >
    > Obviously I must be wrong, could someone explain why?


    Perhaps an analogy is in order here. Consider "the whole file" as "the
    year's food shopping".

    Reading the whole file in one go would be the equivalent of going
    shopping for the whole year's food in one trip. You'd run out of space
    in your house - so it's very inefficient in those terms (storage). You
    might also not end up reading the whole file, which means you'll have
    used up storage (and taken time getting it) very wastefully.

    Reading the file a byte at a time would be the equivalent of going
    shopping for each individual item at a time: if you were having burgers
    for dinner, you would go to the shop for the meat, come back, go to the
    shop for the mustard, come back, etc. That's very inefficient in terms
    of time.

    A buffer is the equivalent of buying about a week's shopping at a time.
    Whenever you run out of food, you do enough shopping to last you for
    some amount of time. That way you don't read *much* more than you're
    going to use, even if you never read the whole file, and you don't end
    up needing to have the whole file in memory at a time.

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet, Jan 15, 2004
    #3
  4. Marc Twain

    Flip Guest

    > Perhaps an analogy is in order here. Consider "the whole file" as "the
    > year's food shopping".

    I've never quite read it explained that way before! I've been accused of
    using analogies a bit too much. :< I like then, helps make things easy to
    understand. This is great! :> I figured the part out about doing the whole
    yrs shopping was bad, but the opposite spectrum is cool too, going to get
    each ingredient is like the byte reads. Thanks.
     
    Flip, Jan 15, 2004
    #4
  5. Marc Twain

    Jon Skeet Guest

    Flip <[remove]> wrote:
    > > Perhaps an analogy is in order here. Consider "the whole file" as "the
    > > year's food shopping".


    > I've never quite read it explained that way before! I've been accused of
    > using analogies a bit too much. :< I like then, helps make things easy to
    > understand. This is great! :> I figured the part out about doing the whole
    > yrs shopping was bad, but the opposite spectrum is cool too, going to get
    > each ingredient is like the byte reads. Thanks.


    I've neither heard it explained like that before, nor done so myself.
    It does seem to work though... This is what comes of posting over
    lunchtime :)

    (It's always fun to unexpectedly come up with a nifty way of explaining
    things...)

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet, Jan 15, 2004
    #5
  6. "Jon Skeet" <> wrote in message
    news:MPG.1a70c4343e6c5d6b989711@10.1.1.42...
    | Flip <[remove]> wrote:
    | > > Perhaps an analogy is in order here. Consider "the whole
    file" as "the
    | > > year's food shopping".
    ...
    | I've neither heard it explained like that before, nor done so
    myself.
    | It does seem to work though... This is what comes of posting
    over
    | lunchtime :)

    Civilisation itself can be explained in terms of lunch.

    The "three stages of civilization" according to
    Douglas Adams..
    "1) Survival,
    2) Inquiry, and
    3) Sophistication.
    These three stages are, in turn, personalized
    by the three questions:
    1) How can we eat?,
    2) Why do we eat?, and
    3) Where shall we have lunch?"

    Having said that, I'd prefer to have a
    dispensing machine and microwave,
    or alternatively, skip lunch completely.
    It's all such a hassle. ;-)

    --
    Andrew Thompson
    * http://www.PhySci.org/ PhySci software suite
    * http://www.1point1C.org/ 1.1C - Superluminal!
    * http://www.AThompson.info/andrew/ personal site
     
    Andrew Thompson, Jan 15, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Krick
    Replies:
    2
    Views:
    14,260
    Marco Schmidt
    Aug 28, 2003
  2. Jimmy Zhang

    reset issue in bufferedInputStream

    Jimmy Zhang, Oct 15, 2003, in forum: Java
    Replies:
    6
    Views:
    780
    Jimmy Zhang
    Oct 15, 2003
  3. Jimmy Zhang
    Replies:
    1
    Views:
    679
    Roedy Green
    Oct 31, 2003
  4. Daniel

    BufferedInputstream problem

    Daniel, Mar 9, 2005, in forum: Java
    Replies:
    7
    Views:
    1,081
    Rogan Dawes
    Mar 10, 2005
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,006
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page