From: crhras on 15 Sep 2006 00:01 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) { } [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Ulrich Eckhardt on 15 Sep 2006 09:51 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()? Well, C stdio streams and C++ IOStreams do different things, I suggest "C++ IOStreams and Locales" by Langer and Kreft for a starter. Else, different IOStreams implementations also differ hugely in performance. > 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. 2. C++ IOStreams perform IO through a plugin architecture, which has some overhead. Further, they do conversions between the external bytes and the internal charset, which might present another overhead. Above mentioned book explains the details of IOStreams so you might want to start there. 3. Opening a file in text mode on systems where that makes a difference might also impact performance negatively. The reason is that you have to do another stage of translation between the disk content and the assumed content. This prevents effective seeking using a memory mapped buffer of equally-sized characters. Lastly, can you give some numbers? What does slow/fast mean? A few percent or a couple hundred percent? Uli [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Thomas Tutone on 15 Sep 2006 09:50 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 > // ----------------- insert the following line here, then rerun your timing test: std::ios::sync_with_stdio(false); > std::string line; > std::ifstream in(filename.c_str()); > > while (std::getline(in, line,'\n')) > { > } Please report back on the result after making that change. Best regards, Tom [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: kanze on 15 Sep 2006 10:04 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()? It obviously depends on the implementation, but... > 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 two programs do radically different things. What happens when you hit a line longer than 511 characters, for example? The important question, of course, isn't which one is faster, but which one is safer and easier to use. Later, if the profiler shows that your code really isn't fast enough, you can change, but typically, it won't make a difference. -- James Kanze GABI Software Conseils en informatique orient?e objet/ Beratung in objektorientierter Datenverarbeitung 9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34 [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Earl Purple on 15 Sep 2006 10:02 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) > { > } If you performed the fstream test first for a big file then immediately did the FILE * test on the same machine with the same file, it is likely that the O/S had cached something because it was a recently read file. That might explain the difference in performance. [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
Next
|
Last
Pages: 1 2 3 4 5 6 Prev: Sanity check: public/private Next: Base Class undefined but header right above it! |