I
Ian Collins
I tried to google "fstream buffer" with great confusion and little
understanding of what I'd need to do to change the code below to use
explicit buffers: hoping to get a better performance match to my old
processing.
I'd like to use C++ strings and I/o streams if possible. but I wonder
if the overhead to use these functions can match older C processing
(fgets, setbuf, etc.) at all. The following seems probable:
1. The overhead to size and allocate/release a string variable for each
logical record will always be slower than use of an explicit char
[some_size] for the text string being read from the input file.
2. The "<<" operators are probably much slower than "fputs".
Are my assumptions valid? Are there options or function calls I can
use to specify fstream buffers (and how are they used?)? Please advise.
TIA
//////////////////// Code ////////////////////
string line;
fstream fVar1, fVar2;
fVar1.open("pat12.n00", fstream::in);
if(fVar1.is_open())
{
fVar2.open("test.txt", fstream:ut);
while(getline(fVar1, line))
{
fVar2<< line<< endl;
}
fVar1.close(), fVar2.close();
}
else cout<< "Unable to open file";
//////////////////////////////////////////////
You haven't provided enough information (anywhere in the thread) to get
a meaningful answer.
What is the code that runs so much faster?
Is the data format line or record based?
The code above _is_ the code that runs much slower, and the code that
I'm basing it on uses fgets/fputs and setbuf with a size of 4096
characters/bytes. That is the only distinction between the 2 code
fragments, but including the I/o class definitions I wrote to prepare
and handle the fgets/fputs with *FILE and open/close logic is too much
to post here.
Bottom line: the executing code is only comparing *FILE
fgets/fputs/setbuf with the fstream getline/<< code above. The only
difference I can see if that the absence of a "setbuf" capability with
fstreams is an enormous performance hit...or getline/<< is a terrible
way to to text file I/o. 8<{{
You still haven't provided enough information for anyone to validate
your results.
These things are tricky to to test. For instance, I have to keep using
new files, or use a file > 16GB to avoid the OS file cache. So any code
that compares reading the same file will be biased to the second test.
By the way, on my Solaris box, comparing
while( (fgets( buf, bufSize, from ) ) )
{
lines++;
fputs( buf, to );
}
to
while( std::getline(in, line) )
{
lines++;
out << line << '\n';
}
shows the iostream version to take a little under twice as long as the C
version. Using a custom streambuf with a 32K buffer narrows this slightly.