Why do BufferedInputStream improve performance?

M

Marc Twain

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.
 
G

Gordon Beaton

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
 
J

Jon Skeet

Marc Twain said:
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.
 
F

Flip

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.
 
J

Jon Skeet

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...)
 
A

Andrew Thompson

| > > 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. ;-)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top