1

I'm working on a small program that deals with rather large (4-5 MB), matrix-shaped (numeric values stored as N*M rows and columns) ASCII files:

1 2 3
4 5 6
7 8 9
etc.

I've noticed that the old-style C file input method:

csFile = fopen("file.dat","r");
while(fscanf(csFile, "%lf", &Point)!=EOF) {
}
fclose(csFile);

is much faster than the most basic C++ implementation (230 ms compared to ~1500 ms for a 3MB file that stores about 230k numeric values):

ifstream myfile ("file.dat");
while(myfile >> Point) {
}
myfile.close();

For simplicity's sake, I've omitted data manipulation functions inside the loops, but even these "bare" examples show almost a sevenfold enhancement of the C type I/O. Why is there such a huge performance difference? Is there a faster way to read these kind of files using the C++ streams/functions?

dda
  • 6,030
  • 2
  • 25
  • 34
K.R.
  • 111
  • 2
  • http://stackoverflow.com/questions/605839/c-and-c-file-i-o – Sebastian Paaske Tørholm Aug 17 '12 at 15:11
  • http://stackoverflow.com/questions/1924530/mixing-cout-and-printf-for-faster-output http://stackoverflow.com/questions/1736267/c-cout-printing-slowly – BoBTFish Aug 17 '12 at 15:12
  • Is 230ms sufficiently fast for you? If so, is there a reason you don't want to use `fscanf`? – Mark Ransom Aug 17 '12 at 16:15
  • I can always aspire for less. As for a particular reason to use C++ streams - there really isn't one (it's only a small project of mine). I guess I would appreciate to employ the advantages of C++ in a C++ project but that isn't really an issue. It's more of a curiosity as to why the input rate difference between these two methods is so vast. – K.R. Aug 17 '12 at 16:58

2 Answers2

3

If you're seeing such a huge difference between C and C++ code, I suspect you're not compiling with optimization. Try using -O3 or whatever flags your compiler needs to enable optimization. Measuring the speed of unoptimized code is generally pointless, as it tells you more about the complexity of the language than the complexity of the code. C++ in particular is very dependent on optimization to get reasonable performance.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
-2

It may related to buffers, try to set enough buffer for your ifstream by function:

file.rdbuf()->pubsetbuf()

This would reduce disk IO and system calls. (read in batch mode)

Baiyan Huang
  • 6,463
  • 8
  • 45
  • 72