From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 4565 invoked by alias); 26 Jun 2003 16:32:42 -0000 Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner@cygwin.com Mail-Followup-To: cygwin@cygwin.com Received: (qmail 4558 invoked from network); 26 Jun 2003 16:32:41 -0000 Received: from unknown (HELO main.gmane.org) (80.91.224.249) by sources.redhat.com with SMTP; 26 Jun 2003 16:32:41 -0000 Received: from list by main.gmane.org with local (Exim 3.35 #1 (Debian)) id 19VZfZ-0003YM-00 for ; Thu, 26 Jun 2003 18:32:21 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: cygwin@cygwin.com Received: from news by main.gmane.org with local (Exim 3.35 #1 (Debian)) id 19VZfX-0003YC-00 for ; Thu, 26 Jun 2003 18:32:19 +0200 From: "Alex Vinokur" Subject: Comparative performance : Reading contents from file into one string Date: Thu, 26 Jun 2003 19:21:00 -0000 Message-ID: References: X-Complaints-To: usenet@main.gmane.org X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4522.1200 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200 X-SW-Source: 2003-06/txt/msg01234.txt.bz2 Here are some C/C++ Performance Tests performed with using C/C++ Program Perfometer. http://sourceforge.net/projects/cpp-perfometer http://alexvn.freeservers.com/s1/perfometer.html Reading contents from file into one string ------------------------------------------ =========================================== Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927 (prerelease) =========================================== ################ Comparative performance : Summary ################ #================================================================== # Read contents from file into one string #------------------------------------------------------------------ # Resource Name : user time used (via rusage) # Resource Cost Unit : milliseconds (unsigned long long) # Resource State Unit : timeval #================================================================== : ---------------------------------------------------------------------- : ReadFile1 : getline (size = 0) -> 43 : ReadFile1 : getline (size = 10) -> 63 : ReadFile1 : getline (size = 100) -> 53 : ReadFile1 : getline (size = 1000) -> 86 : ReadFile1 : getline (size = 10000) -> 397 : ReadFile1 : getline (size = 100000) -> 3892 : ReadFile2 : vector, reading char (size = 0) -> 53 : ReadFile2 : vector, reading char (size = 10) -> 53 : ReadFile2 : vector, reading char (size = 100) -> 40 : ReadFile2 : vector, reading char (size = 1000) -> 80 : ReadFile2 : vector, reading char (size = 10000) -> 304 : ReadFile2 : vector, reading char (size = 100000) -> 3227 : ReadFile3 : string, reading char (size = 0) -> 76 : ReadFile3 : string, reading char (size = 10) -> 86 : ReadFile3 : string, reading char (size = 100) -> 73 : ReadFile3 : string, reading char (size = 1000) -> 80 : ReadFile3 : string, reading char (size = 10000) -> 237 : ReadFile3 : string, reading char (size = 100000) -> 2046 : ReadFile4 : vector, reading whole file (size = 0) -> 60 : ReadFile4 : vector, reading whole file (size = 10) -> 87 : ReadFile4 : vector, reading whole file (size = 100) -> 86 : ReadFile4 : vector, reading whole file (size = 1000) -> 110 : ReadFile4 : vector, reading whole file (size = 10000) -> 170 : ReadFile4 : vector, reading whole file (size = 100000) -> 1378 : ReadFile5 : mmap (size = 0) -> 23 : ReadFile5 : mmap (size = 10) -> 20 : ReadFile5 : mmap (size = 100) -> 40 : ReadFile5 : mmap (size = 1000) -> 23 : ReadFile5 : mmap (size = 10000) -> 33 : ReadFile5 : mmap (size = 100000) -> 83 : ReadFile6 : iterator (size = 0) -> 56 : ReadFile6 : iterator (size = 10) -> 57 : ReadFile6 : iterator (size = 100) -> 40 : ReadFile6 : iterator (size = 1000) -> 73 : ReadFile6 : iterator (size = 10000) -> 393 : ReadFile6 : iterator (size = 100000) -> 4713 : ---------------------------------------------------------------------- ################################################################### ################ C++ code : BEGIN ################ // ======================================== // ---------------------------------------- // Functions ReadFile1 - ReadFile6 are // lightly changed functions from the arctile : // -------- // From: "Tom Hines" // Subject: Re: How to efficiently read contents from file into one string? // Newsgroup: comp.lang.c++.moderated // Date: Thursday, January 30, 2003 3:55 PM // ---------------------------------------- // --------- static void ReadFile1 (const string& filename_i, unsigned long& str_size_o) { // --- Using getline() --- string line, str; ifstream infile (filename_i.c_str()); assert (infile.is_open()); while (getline (infile, line)) { str.append(line); line.erase(); } str_size_o = str.size(); } // --------- static void ReadFile2(const string& filename_i, unsigned long& str_size_o) { // --- Using vector char at a time --- ifstream infile (filename_i.c_str()); assert (infile.is_open()); vector v; char ch; while (infile.get(ch)) if (ch != '\n') v.push_back(ch); string str (v.empty() ? string() : string (v.begin(), v.end())); str_size_o = str.size(); } // --------- static void ReadFile3(const string& filename_i, unsigned long& str_size_o) { // --- Using string, char at a time --- ifstream infile (filename_i.c_str(), ios::in | ios::ate); assert (infile.is_open()); streampos sz = infile.tellg(); infile.seekg(0, ios::beg); char ch; string str(sz, '0'); int i = 0; for (i = 0; infile.get(ch); ) if (ch != '\n') str[i++] = ch; str.erase (i); str_size_o = str.size(); } // --------- static void ReadFile4(const string& filename_i, unsigned long& str_size_o) { // --- Using vector, reading whole file at once --- ifstream infile (filename_i.c_str(), ios::in | ios::ate); assert (infile.is_open()); streampos sz = infile.tellg(); infile.seekg(0, ios::beg); vector v(sz); infile.read(&v[0], sz); // v.erase(remove(v.begin(), v.end(), '\n'), v.end()); string str (v.empty() ? string() : string (v.begin(), v.end()).c_str()); str_size_o = str.size(); } // --------- static void ReadFile5(const string& filename_i, unsigned long& str_size_o) { // --- Using mmap --- int fd = open(filename_i.c_str(), O_RDONLY); assert (fd > 2); off_t sz = lseek(fd, 0, SEEK_END); char* ptr = (char*)mmap(0, sz, PROT_READ, MAP_SHARED, fd, 0); str_size_o = 0; if (ptr == MAP_FAILED) { close(fd); return; } assert (ptr != MAP_FAILED); string str(ptr, ptr + sz); munmap(ptr, sz); // str.erase(remove(str.begin(), str.end(), '\n'), str.end()); // str.erase(remove(str.begin(), str.end(), '\r'), str.end()); close(fd); str_size_o = str.size(); } // --------- static void ReadFile6 (const string& filename_i, unsigned long& str_size_o) { // --- Using iterator --- ifstream infile (filename_i.c_str()); assert (infile.is_open()); infile >> noskipws; istream_iterator iter(infile), eos; string str(iter, eos); // str.erase(remove(str.begin(), str.end(), '\n'), str.end()); str_size_o = str.size(); } ################ C++ code : END ################## ========================================== Alex Vinokur mailto:alexvn@connect.to http://www.simtel.net/pub/oth/19088.html http://sourceforge.net/users/alexvn ========================================== -- Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple Problem reports: http://cygwin.com/problems.html Documentation: http://cygwin.com/docs.html FAQ: http://cygwin.com/faq/