From: crhras on 16 Sep 2006 07:50 { Quoted clc++m banner removed. -mod } "alex" <alex.shulgin(a)gmail.com> wrote in message news:1158333704.094313.148570(a)d34g2000cwd.googlegroups.com... > crhras wrote: >> 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) >> { >> } > > The code blocks above are not completely equivalent. You can re-test > with this block instead: > > std::ifstream in(filename.c_str()); > char line[512]; > while (in.getline(line, 512)) > { > } > > BTW, specifying precise test conditions may somewhat clarify you point. > > > Alex Thanks for your response. I tested your suggestion and it seemed to take about 25% less time. Based on the results it seems to do the same as adding the line std::ios::sync_with_stdio(false); which was a suggestion in another post. [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: ShaoVie@gmail.com on 16 Sep 2006 08:00 $ yes dddd > cui ls -l cui -rw-rw-r-- 1 ace ace 130027520 Sep 16 16:53 cui ------------------------------------------------------------------------------------------------------ #include <fstream> #include <cstdio> using namespace std; int main () { #ifndef FILE_C ifstream in("cui", ios::in); char ch[80]; while (in.getline (ch, 80)) ; in.close (); #else FILE *fp = fopen ("cui", "r"); char ch[80]; while (fgets (ch, 80, fp)) ; fclose (fp); #endif } my code ------------------------------------------------------ g++ -o test test.cpp $time ./test real 0m3.782s user 0m3.650s sys 0m0.140s ----------------------------------------------------- g++ -DFILE_C -o test test.cpp $time ./test ..... real 0m4.539s user 0m4.420s sys 0m0.120s ------------------------------------------------------- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Seungbeom Kim on 16 Sep 2006 08:01 crhras wrote: > 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) > { > } Strange. What environment did you run the tests on? On Linux-2.6.8, gcc-3.3.5 and libstdc++5-3.3-dev, the fgets version gives real 0m0.020s user 0m0.016s sys 0m0.005s while the getline version gives real 0m0.006s user 0m0.004s sys 0m0.002s on a 3.6 MB test file. This may not always be true, but can at least indicate that getline is not always slower than fgets. In addition, adding std::ios_base::sync_with_stdio(false) at the beginning did not alter the result significantly. -- Seungbeom Kim [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Jeff Koftinoff on 16 Sep 2006 13:47 Ulrich Eckhardt wrote: > crhras wrote: <snip> > > 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) > > { > > } > > These two snippets are not comparable: > 1. std::getline() dynamically resizes the string to fit the input. <snip> This is an important feature... However it can be a big problem that std::getline() function can not be told what maximum length to allow. In actual fact on most platforms, giving a 2 Gigabyte file (with no '\n' in it) will cause the program to crash (with no exception thrown), precisely because of this feature of dynamically resizing the string to fit the input with no upper bounds. Depending where this code is used, it can be the basis of a security hole - and in that sense fgets() could be a better solution! Jeff Koftinoff www.jdkoftinoff.com [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: shakahshakah@gmail.com on 16 Sep 2006 13:51
{ Quoted clc++m banner removed. Yes, it's nice that the banner is so popular. But could people please stop quoting it all the time? -mod } Seungbeom Kim wrote: > crhras wrote: > > 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) > > { > > } > > Strange. What environment did you run the tests on? > On Linux-2.6.8, gcc-3.3.5 and libstdc++5-3.3-dev, the fgets version gives > > real 0m0.020s > user 0m0.016s > sys 0m0.005s > > while the getline version gives > > real 0m0.006s > user 0m0.004s > sys 0m0.002s > > on a 3.6 MB test file. This may not always be true, but can > at least indicate that getline is not always slower than fgets. > > In addition, adding std::ios_base::sync_with_stdio(false) at the > beginning did not alter the result significantly. FWIW, I get the following results consistently on Linux (fgets about 40% faster than getline): jc(a)re1-dev:/tmp$ uname -a Linux re1-dev 2.6.17-1.2145_FC5 #1 SMP Sat Jul 1 13:05:01 EDT 2006 x86_64 x86_64 x86_64 GNU/Linux jc(a)re1-dev:/tmp$ g++ --version g++ (GCC) 4.1.1 20060525 (Red Hat 4.1.1-1) Copyright (C) 2006 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. jc(a)re1-dev:/tmp$ l /tmp/biggestfile -rw-r--r-- 1 jc ccbrk 831993344 Sep 16 14:34 /tmp/biggestfile jc(a)re1-dev:/tmp$ time ./tt /tmp/biggestfile fgets file: /tmp/biggestfile mode: fgets 7999936 lines read in 1559 msec real 0m1.560s user 0m0.984s sys 0m0.576s jc(a)re1-dev:/tmp$ time ./tt /tmp/biggestfile getline file: /tmp/biggestfile mode: getline 7999936 lines read in 2518 msec real 0m2.520s user 0m2.004s sys 0m0.516s [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |