Buffering in libstc++ 3.0
Peter Simons
simons@cryp.to
Wed Aug 29 10:22:00 GMT 2001
Hi everybody,
I have experienced some weird effects with g++ 3.0 and hope that you
guys can explain to me what I am doing wrong. I compiled the program
| #include <iostream>
| using namespace std;
|
| int main(int argc, char** argv)
| {
| #ifdef NO_STDIO_SYNC
| ios::sync_with_stdio(false);
| #endif
| #ifdef USE_MY_BUFFER
| char buf[1*1024];
| cin.rdbuf()->pubsetbuf(buf, sizeof(buf));
| #endif
|
| string line;
| for (getline(cin, line); cin; getline(cin, line))
| ;
|
| return 0;
| }
in three versions: With no defines, with NO_STDIO_SYNC defined, with
USE_MY_BUFFER defined, and with both labels defined. Then I time(1)ed
the program reading a 50000-line file, getting the following results:
| simons@peti:/tmp$ time ./test-plain <input
| real 0m18.941s
| user 0m13.350s
| sys 0m2.880s
|
| simons@peti:/tmp$ time ./test-no_stdio_sync <input
| real 0m18.187s
| user 0m13.300s
| sys 0m2.590s
|
| simons@peti:/tmp$ time ./test-my_buffer <input
| real 0m3.795s
| user 0m3.340s
| sys 0m0.030s
|
| simons@peti:/tmp$ time ./test-my_buffer-no_stdio_sync <input
| real 0m4.862s
| user 0m3.300s
| sys 0m0.050s
|
| simons@peti:/tmp$ time ./test-my_buffer-no_stdio_sync <input
| real 0m4.054s
| user 0m3.350s
| sys 0m0.020s
It seems that switching the synchronization between std::cin and stdio
off doesn't have hardly any effect at all -- contrary to what I read
in the FAQ. Furthermore, it seems as if the default instance of
std::cin does not have buffering enabled at all! Why is this so?
Does anyone know any other mechanisms that would further speed up I/O
with streams? I noticed that compiling with -O3 works wonders, but I
am sure there are more things one could do to increase the
performance.
-peter
More information about the Libstdc++
mailing list