tags:

views:

264

answers:

6

I was thinking about this when I ran into a problem using std::ofstream.

My thinking is that since std::ifstream, it wouldn't support random access. Rather, it would just start at the beginning and stream by until you get to the part you want. Is this just quick so we don't notice?

And I'm pretty sure FILE* supports random access so this would be fast as well?

+4  A: 

ifstream supports random access with seekg. FILE* might be faster but you should measure it.

stribika
+4  A: 

Since both of them imply system calls and that is going to be some orders of magnitude more time consuming that the rest of the operation, the performance of both should be very similar.

Artur Soler
I agree that the performance might be similar but this doesn't contradict with one being faster so I don't think this answers the question whatsoever.
Mehrdad Afshari
A: 

Remember that random I/O will defeat any caching done in the underlying APIs. And it is NOT going to be faster to read until you reach a particular location than to seek, regardless of which mechanism you use (assuming that your files are of any significant size).

I'm with stribika here: Measure then make a decision.

Larry Osterman
+4  A: 

Let's assume that FILE* was faster. Now can you give me just one good reason why std::ifstream shouldn't be implemented in terms of that? Which means that performance becomes similar.

I'll leave the opposite case (if std::ifstream was faster) as an exercise to the reader (hint, the same is the case there).

Before worry about performance, there is one rule of thumb you should always keep in mind:

The people who wrote your language's standard library have at least 4 working brain cells. They are not stupid.

This implies that if feature X can be trivially implemented in terms of feature Y, then X will not be noticeably slower than Y.

jalf
Sorry, but sometimes even they are stupid.
Magnus Skog
Got an example?
jalf
Very seldom are they stupid. However, they have to balance many different aspects, including full standards compliance, backwards compatibility (can't break peoples code, even if its wrong), an existing codebase for the library, time, budget and manpower constraints, etc. They may also be optimizing for different things: size vs. speed, a different use of the code, etc. As such they may make different trade-offs than we would prefer in a particular situation.
KeithB
A: 

std::ifstream is a wrapper around FILE, so the former is no way faster then the latter. How much do they differ depends on the compiler and whether it can inline wrapper function calls and perform other optimizations. Besides, reading formatted data from C++ streams is slower because they work with locales and such stuff.

However, if you often call for random access, this would be the bottleneck, as the other answers state. Anyway, the best thing is to use a profiler and measure your app performance.

Dmitry Risenberg
This is not true in every implementation. There is no requirement to use FILE for ifstream.
Zan Lynx
Sure it is not required to be this way, but msvc and g++ do it, and I think it is reasonable to assume that most implementations will do the same.
Dmitry Risenberg
No, g++ does not do it this way. The last time I looked into the libstdc++ code for GCC 4.1 it used direct POSIX read/write for file IO.
Zan Lynx
+2  A: 

If you run speed comparisons on standard input or output, remember to call std::ios_base::sync_with_stdio(false) before. When this setting is true, then all operations are done so that reads from std::cin pull data from the same buffers as fgets(stdin). Setting it to false gives iostreams more freedom and less bookkeeping.

Zan Lynx