Re: fgets() vs std::getline() performance
{ Quoted clc++m banner removed. Wrapped lines fixed up. Because I had
the time. ;-) -mod }
"crhras" <crhras@sbcglobal.net> wrote in message
news:TkoOg.1329$e66.993@newssvr13.news.prodigy.com...
Wow ! I just used two different file IO methods and the performance
difference was huge. Is there something that I am doing wrong? or is
fgets() just that much faster than getline()?
Here's the code I used :
// SLOOOOOOW
// -----------------
std::string line;
std::ifstream in(filename.c_str());
while (std::getline(in, line,'\n'))
{
}
// FAAAAAASSSSSTTTTT
// ---------------------------
FILE * fp;
fp = fopen(filename.c_str(), "r");
while (fgets(line, 512, fp) != NULL)
{
}
Thank you for the responses. I went back to the drawing board using
your suggestions. Some of the results are in this post and others will
be posted under the specific newsgroup response which I was testing.
First off, the data file used contains 3.5 million text records of
varying lengths terminated by '\n'.
I reran the tests with some timers to tell exactly how long each case is
taking. I ran each test twice in sequence to make sure that caching was
not responsible for the time difference.
-----------------------------------
// Test 1
while (std::getline(in, line,'\n')) { }
-----------------------------------
// Test 2
while (fgets(cline, 512, fp) != NULL) { }
Results :
Test 1 has taken 203 seconds.
Test 2 has taken 5 seconds.
Test 1 has taken 201 seconds.
Test 2 has taken 4 seconds.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]