views:

118

answers:

4

I need to read fixed number of bytes from files, whose sizes are around 50MB. To be more precise, read a frame from YUV 4:2:0 CIF/QCIF files (~25KB to ~100KB per frame). Not very huge number but I don't want whole file to be in the memory. I'm using C++, in such a case, which of FILE* or ifstream has better (less/minimal) memory usage? Please kindly advise. Thanks!

EDIT:

I read fixed number of bytes: 25KB or 100KB (depending on QCIF/CIF format). The reading is in binary mode and forward-only. No seeking needed. No writing needed, only reading.

EDIT:

If identifying better of them is hard, which one does not require loading the whole file into memory?

+4  A: 

Impossible to say - it will depend on the implementation, and how you are reading the data, which you have not described. In general, questions here regarding performance are somewhat pointless, as they depend heavily on your actual usage of library and language features, the specific implementation, your hardware etc. etc.

Edit: To answer your expanded question - neither library requires you read everything into memory. Why would you think they do?

anon
Even after I read the whole file? I only store 50KB or 200KB (only 2 frames) in buffer. Anything else is not stored.
Viet
@Viet: Every implementation I'm aware of keeps a fixed size buffer for performance, but they aren't going to keep everything you've read.
Matthew Crumley
@Vliet Both libraries will do some buffering (implementation dependent) but they will discard the buffered data as required. I really don't understand your basic issue here - perhaps you could post some code indicating where your concerns lie.
anon
+2  A: 

I think the best answer would be "profile and see", but in theory FILE* should be more efficient in time and memory usage. Streams do add different wrappers, error handlers, etc, etc, etc... over raw reading / writing routines, that could (in your particular case) affect the memory usage.

Kotti
I doubt any overhead of an `ifstream` object over a `FILE` struct is going to be a significant factor.
Matthew Crumley
even more importantly, that overhead is almost certainly a constant, and will not increase with how large the file is or how much you are reading (except perhaps the internal buffer, but even that I doubt will grow past a certain point).
rmeador
+1  A: 

Performance wise you're definitely better of with FILE* (I profiled that some time ago in a project of mine). Memory wise iostreams shouldn't pose a big problem, although I think that there is some overhead as it wraps the C library.

milan1612
+1  A: 

You can expect a smaller executable using FILE*, since its supporting libraries are simpler than ifstream, but the other factors (runtime memory consumption and performace) rarely make a significant difference. But the small gain will be in general towards FILE*, again merely because it's simpler.

If the processing you do with the file is very basic and/or you don't need to parse a text input file, FILE* will suit you well. On the other hand, if the opposite is true, I'd go for ifstream - I find the >> operator a lot handier and safer than using fscanf.

Fabio Ceconello