Alex Vinokur said:
Testing "Reading contents from file into one string" with using C/C++ Perfometer.
Summary
http://groups-beta.google.com/group/perfo/msg/5801f89772746dc3
http://groups-beta.google.com/group/perfo/attach/5801f89772746dc3/perfo_summary_file2str.txt?part=2
========================================================
| | File size |
| Testsuite |------------------------|
| | 1000 : 10000 : 100000 |
|------------------------------------------------------|
| getline | 125 : 839 : 7729 |
| vector, reading char | 75 : 592 : 5816 |
| string, reading char | 71 : 570 : 5626 |
| vector, reading whole file | 16 : 30 : 146 |
| mmap (UNIX) | 13 : 18 : 30 |
| istream_iterator | 80 : 597 : 6028 |
| ostringstream, rdbuf | 15 : 20 : 66 |
| istreambuf_iterator | 30 : 71 : 624 |
========================================================
We can see that speed differences between the testsuites are sizeable.
Full raw run log:
http://groups-beta.google.com/group/log-files/msg/1f7160243fd793cb
http://groups-beta.google.com/group/log-files/attach/1f7160243fd793cb/perfo_log_file2str.txt?part=2
[snip]
Extended set of testsuites for "Reading contents from file into one string"
contains 29 testsuites:
* 4 - for C language,
* 1 - for UNIX system calls,
* 24 - for C++ language.
Testing "Reading contents from file into one string"
with using Simple C/C++ Perfometer.
---------------------------
*
http://groups-beta.google.com/group/perfo/msg/8273f4d1a05cfbd1
*
http://article.gmane.org/gmane.comp.lang.c++.perfometer/110
*
http://permalink.gmane.org/gmane.comp.lang.c++.perfometer/110
*
http://comments.gmane.org/gmane.comp.lang.c++.perfometer/110
*
http://cache.gmane.org/gmane/comp/lang/c++/perfometer/110
---------------------------
Environment: Windows 2000, Cygwin
File size = 10000
Test results sorted by ascending time used
(the best of time used in binary and text openmode).
===========================================================================
| Testsuite | File OpenMode |
|-------------------------------------------------------|-----------------|
| Code : Name | binary : text |
|-------------------------------------------------------|-----------------|
| C-04 : C-function fread, max size buffer | 10 : 10 |
| C-03 : C-function fread, const size buffer | 10 : 13 |
| CPP-24 : std::string and istream::read | 10 : 16 |
| Unix-C-05 : UNIX system call mmap | 13 : 53 |
| CPP-05 : istream::read, ostream::write, | 20 : 30 |
| : const size buffer | : |
| CPP-06 : istream::read, ostream::write, | 20 : 30 |
| : ostringstream, const size buffer | : |
| CPP-04 : ifstream::rdbuf, ostream:

perator<< | 20 : 33 |
| CPP-08 : istream::read, ostream::write | 23 : 30 |
| : max size buffer | : |
| CPP-03 : streambuf::sbumpc, ostream:

perator<< | 26 : 30 |
| CPP-23 : std::vector, istream::read | 33 : 43 |
| CPP-07 : istream::readsome, ostream::write | 53 : 60 |
| : const size buffer | : |
| CPP-11 : istream::getline, ostringstream | 56 : 57 |
| : ostream:

perator<< | 56 : 57 |
| CPP-14 : istream::get(char*, streamsize), | 56 : 57 |
| : ostream:

perator<<, const size | : |
| CPP-15 : istream::get(streambuf&), streambuf, | 163 : 76 |
| : ostream:

perator<< | : |
| CPP-20 : istreambuf_iterator, std::string | 193 : 167 |
| CPP-18 : istreambuf_iterator, ostreambuf_iterator, | 187 : 370 |
| : std::copy | : |
| CPP-19 : istreambuf_iterator, ostreambuf_iterator, | 270 : 190 |
| : std::transform | : |
| CPP-13 : istream::get(char) | 1442 : 1428 |
| CPP-22 : std::vector, push_back() | 1502 : 1452 |
| CPP-17 : istream_iterator, std::string | 1542 : 1592 |
| CPP-09 : std::getline, ostringstream, | 1656 : 1619 |
| : ostream:

perator<< | 1656 : 1619 |
| CPP-02 : streambuf::sbumpc | 1625 : 1652 |
| CPP-10 : std::getline, std::string, | 1665 : 1652 |
| : ostream:

perator<< | : |
| C-02 : C-function fgetc | 1939 : 1966 |
| C-01 : C-function getc | 2002 : 1983 |
| CPP-21 : std::vector, std::copy | 2827 : 2890 |
| CPP-12 : istream::get(char), ostream:

ut | 2830 : 2841 |
| CPP-01 : istream:

perator>> | 2951 : 2957 |
| CPP-16 : istream_iterator, ostream_iterator, | 4232 : 4269 |
| : std::copy | : |
===========================================================================
We can see that the best method of the C++ methods is CPP-24
that uses std::string and istream::read()
====== CPP-24 method ======
string str (infile_size, '0');
infile.read(&ret_str[0], infile_size);
return str;
===========================