public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: "Alex Vinokur" <alexvn@connect.to>
To: cygwin@cygwin.com
Subject: Comparative performance : Reading contents from file into one string
Date: Thu, 26 Jun 2003 19:21:00 -0000	[thread overview]
Message-ID: <bdf76f$d94$1@main.gmane.org> (raw)
In-Reply-To: <bdc6ug$qsv$1@main.gmane.org>


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@yahoo.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@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/

      parent reply	other threads:[~2003-06-26 16:32 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-06-25 15:17 assertion "ptr != MAP_FAILED" failed while using mmap Alex Vinokur
2003-06-25 16:51 ` Elfyn McBratney
2003-06-25 16:55   ` Ronald Landheer-Cieslak
2003-06-25 21:40     ` Elfyn McBratney
2003-06-26  5:06       ` Igor Pechtchanski
2003-06-26  6:08         ` Alex Vinokur
2003-06-26  8:31           ` Alex Vinokur
2003-06-26  9:53             ` Brian Dessent
2003-06-26 11:33               ` Alex Vinokur
2003-06-26  9:23           ` Brian Dessent
2003-06-26 19:21 ` Alex Vinokur [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='bdf76f$d94$1@main.gmane.org' \
    --to=alexvn@connect.to \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).