tags:

views:

72

answers:

4

Hello.

c++ question.

for(i=1;i<10000;i++){
    cout << myfile.get();
}

Will program make 10000 IO operations on the file in HDD? (given that file is larger) If so, maybe it is better to read lets say 512 bytes to some buffer and then take char by char from there and then again copy 512 bytes and so on?

+1  A: 

Your OS will cache the file, so you shouldn't need to optimize this for common use.

gruszczy
+1  A: 

ifstream is buffered, so, no.

KennyTM
It won't make 10000 IO requests, but it'll still be a fair bit slower than reading those 10KB in one call.
jalf
+1  A: 

Try it.

However, in many cases, the fastest operation will be to read the whole file at once, and then work on in-memory data.

But really, try out each strategy, and see what works best.

Keep in mind though, that regardless of the underlying file buffering mechanism, reading one byte at a time is slow. If nothing else, it calls the fairly slow IOStreams library 10000 times, when you could have done just a couple of calls.

jalf
+2  A: 

As others have said - try it. Tests I've done show that reading a large block in one go (using streams) can be up to twice as fast as depending solely on the stream's own buffering. However, this is dependent on things like buffer size and (I would expect) stream library implementation - I use g++.

anon