From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fa528,e8460df81e913ac3,start X-Google-Attributes: gidfa528,public X-Google-ArrivalTime: 2001-02-20 04:03:31 PST From: "Alexandre Masselot" Newsgroups: comp.unix.tru64 Subject: reading a file slooooooow with c++ getline Date: Tue, 20 Feb 2001 13:02:55 +0100 Organization: Swisscom IP+ (post doesn't reflect views of Swisscom) Message-ID: <96tme9$ma2$1@rex.ip-plus.net> NNTP-Posting-Host: 194.209.13.177 X-Trace: rex.ip-plus.net 982670601 22850 194.209.13.177 (20 Feb 2001 12:03:21 GMT) X-Complaints-To: abuse@ip-plus.net NNTP-Posting-Date: Tue, 20 Feb 2001 12:03:21 +0000 (UTC) User-Agent: Pan/0.9.1 (Unix) X-No-Productlinks: Yes Path: supernews.google.com!sn-xit-02!supernews.com!isdnet!golmote!Amsterdam.Infonet!News.Amsterdam.UnisourceCS!newsfeed-zh.ip-plus.net!gg-pc810!alex Xref: supernews.google.com comp.unix.tru64:1742 Comparing reading times on an es/40 with two techinques gives me dramatic timings. The c++ (compiled with cxx) version with getline(ifstream,is) is 50 times slower than the c version with fgets. The c++ version is even 6 times slower on a es/40 than on my pc with g++ Ok, reading on a string can be a bit slower than directly to a char buffer, but a factor 50 slower??? I'm rather new to c++, so I may miss a big issue. And i feel rather comfortable to manipulate strings in this case. thanks for your help Alex ps below c++ and c version, code and timing /tmp/a.txt is a simple text file (~10M) The c++ alex@gm-mastdev:misc>cxx -V Compaq C++ V6.3-002 for Compaq Tru64 UNIX V5.1 (Rev. 732) Compiler Driver V6.3-002 (cxx) cxx Driver #include #include #include int main(){ ifstream is("/tmp/a.txt"); string s; while(!is.eof()){ getline(is,s); } is.close(); return 0; } alex@gm-mastdev:misc>!cxx cxx -O4 read.cpp -o read-cpp alex@gm-mastdev:misc>time ./read-cpp real 0m16.637s user 0m16.347s sys 0m0.146s the C version alex@gm-mastdev:misc>cc -V Compaq C V6.3-027 on Compaq Tru64 UNIX V5.1 (Rev. 732) Compiler Driver V6.3-026 (sys) cc Driver #include int main(int argc, char *argv){ int BlockSize=8; FILE *pf1; char BUF[32*1024+1]; int i,S=1024*BlockSize; pf1=fopen("/tmp/a.txt","r"); fgets(BUF,S,pf1); while(!feof(pf1)){ fgets(BUF,S,pf1); } fclose(pf1); exit (0); } cc -O4 read.c -o read-c alex@gm-mastdev:misc>time ./read-c real 0m0.357s user 0m0.043s sys 0m0.064s