Mail Archives: cygwin/2003/06/26/12:32:56
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" <tom_hines AT yahoo DOT com>
// 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<char> 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<char> 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<char> 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 AT connect DOT 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/
- Raw text -