Цитата(Litkevich Yuriy @ 13.2.2008, 17:33)
скинь коль уж начал
Дык не вопрос, совершенно не вопрос...
По ссылке можно найти и результаты тестирования в том числе, в этом сообщении сами исходники
http://groups.google.com/group/perfo/brows...4466f69541c2e63===================== 4. Methods of copying : BEGIN =====================
FILE* ifp; // input file pointer
FILE* ofp; // output file pointer
int ifd; // input file descriptor; UNIX
int ofd; // output file descriptor; UNIX
ifstream ifs; // input file stream
ofstream ofs; // output file stream
// -----
// Note. Files are tested in both modes text and binary
// -----
char ch;
int ich;
const int newline_int_symbol (int ('\n'));
size_t nread;
streamsize len;
size_t no_of_file_bytes; // File size (number of bytes)
size_t no_of_file_lines; // Number of file lines
// -----
// Note. 'no_of_file_bytes' doesn't contain number of '\r' in '\r\n'
// -----
char const_buf [4096];
char* max_buf = new char [no_of_file_bytes + no_of_file_lines + 1];
string line;
string str;
ostringstream oss;
struct char_identity { char operator()(char ch) const { return ch; } };
### C-01: C-Functions getc() and putc()
------------------------------------------------
while ((ich = getc(ifp)) != EOF) putc(ich, ofp);
------------------------------------------------
### C-02: C-Functions fgetc() and fputc()
------------------------------------------------
while ((ich = fgetc(ifp)) != EOF) fputc(ich, ofp);
------------------------------------------------
### C-03: C-Functions fread() and fwrite() - with const size buffer
------------------------------------------------
while ((nread = fread(const_buf, sizeof(char), sizeof(const_buf), ifp)) > 0)
{
fwrite(const_buf, sizeof(char), nread, ofp);
}
------------------------------------------------
### C-04: C-Functions fread() and fwrite() - with max size buffer
------------------------------------------------
while ((nread = fread(max_buf, sizeof(char), sizeof(max_buf), ifp)) > 0)
{
fwrite(max_buf, sizeof(char), nread, ofp);
}
------------------------------------------------
### Unix-C-05: UNIX system call mmap()
------------------------------------------------
size_t actual_file_size = no_of_file_bytes + ((text-mode) ? no_of_file_lines : 0)
char* ptr = (char*)mmap(0, actual_file_size, PROT_READ, MAP_SHARED, ifd, 0);
write(ofd, ptr, actual_file_size);
munmap(ptr, actual_file_size);
------------------------------------------------
### CPP-01: istream::operator>> and ostream::operator<<
------------------------------------------------
ifs.unsetf(ios::skipws);
while (ifs >> ch) ofs << ch;
------------------------------------------------
### CPP-02: streambuf::sbumpc() and streambuf::sputc()
------------------------------------------------
while ((ch = ifs.rdbuf()->sbumpc()) != EOF) ofs.rdbuf()->sputc(ch);
------------------------------------------------
### CPP-03: streambuf::sbumpc() and ostream::operator<<
------------------------------------------------
ch = ifs.rdbuf()->sbumpc();
ofs << ch;
while (ch != EOF)
{
ofs << ifs.rdbuf();
ch = ifs.rdbuf()->sbumpc();
}
------------------------------------------------
### CPP-04: ifstream::rdbuf() and ostream::operator<<
------------------------------------------------
ofs << ifs.rdbuf();
------------------------------------------------
### CPP-05: istream::read() and ostream::write() - with const size buffer
------------------------------------------------
while (!ifs.eof())
{
ifs.read (const_buf, sizeof(const_buf));
ofs.write (const_buf, ifs.gcount());
}
------------------------------------------------
### CPP-06: istream::read() and ostream::write(), std::ostringstream, ostream::operator<< - with const buffer
------------------------------------------------
while (!ifs.eof())
{
ifs.read (const_buf, sizeof(const_buf));
oss.write (const_buf, ifs.gcount());
}
ofs << oss.str();
------------------------------------------------
### CPP-07: istream::readsome() and ostream::write() - with const size buffer
------------------------------------------------
do
{
len = ifs.readsome (const_buf, sizeof(const_buf));
ofs.write (const_buf, len);
} while (len);
------------------------------------------------
### CPP-08: istream::read() and ostream::write() - with max size buffer
------------------------------------------------
while (!ifs.eof())
{
ifs.read (max_buf, no_of_file_bytes + no_of_file_lines);
ofs.write (max_buf, ifs.gcount());
}
------------------------------------------------
### CPP-09: std::getline, std::ostringstream, ostream::operator<<
------------------------------------------------
while (getline (ifs, line)) oss << line << '\n';
string tmp(oss.str());
if (!tmp.empty())
{
ifs.rdbuf()->sungetc ();
if (ifs.rdbuf()->sgetc() != '\n') tmp.erase(tmp.size() - 1);
}
ofs << tmp;
------------------------------------------------
### CPP-10: std::getline, std::string, ostream::operator<<
------------------------------------------------
while (getline (ifs, line))
{
str +=line;
str +='\n';
}
if (!str.empty())
{
ifs.rdbuf()->sungetc ();
if (ifs.rdbuf()->sgetc() != '\n') str.erase(str.size() - 1);
}
ofs << str;
------------------------------------------------
### CPP-11: istream::getline, std::ostringstream, ostream::operator<<
------------------------------------------------
while (ifs.getline (const_buf, sizeof(const_buf)).gcount())
{
oss << const_buf;
if (ifs.fail()) ifs.clear (~(ios_base::failbit | ~ifs.rdstate ()));
else oss << '\n';
}
string tmp(oss.str());
if (!tmp.empty())
{
ifs.rdbuf()->sungetc ();
if (ifs.rdbuf()->sgetc() != '\n') tmp.erase(tmp.size() - 1);
}
ofs << tmp;
------------------------------------------------
### CPP-12: istream::get(char) and ostream::put
------------------------------------------------
while (ifs.get(ch)) ofs.put(ch);
------------------------------------------------
### CPP-13: istream::get(char)
------------------------------------------------
for (unsigned long i = 0; ifs.get (ch); i++) str[i] = ch;
str.erase (i);
ofs << str;
------------------------------------------------
### CPP-14: istream::get(char*, streamsize), ostream::operator<< - with const size buffer
------------------------------------------------
while (ifs.get (const_buf, sizeof(const_buf)))
{
ofs << const_buf;
if (ifs.peek() == newline_int_symbol) ofs << char(ifs.get());
}
------------------------------------------------
### CPP-15: istream::get(streambuf&) and std::streambuf, ostream::operator<<
------------------------------------------------
while (ifs.get (*ofs.rdbuf()))
{
if (ifs.peek() == newline_int_symbol) ofs << char(ifs.get());
}
------------------------------------------------
### CPP-16: std::istream_iterator, std::ostream_iterator and std::copy
------------------------------------------------
ifs >> noskipws;
istream_iterator<char> in(ifs), eos;
ostream_iterator<char> out(ofs);
copy (in, eos, out);
------------------------------------------------
### CPP-17: std::istream_iterator, std::string
------------------------------------------------
ifs >> noskipws;
istream_iterator<char> in(ifs), eos;
ofs << string(in, eos);
------------------------------------------------
### CPP-18: std::istreambuf_iterator, std::ostreambuf_iterator and std::copy
------------------------------------------------
ifs >> noskipws;
istreambuf_iterator<char> in(ifs), eos;
ostreambuf_iterator<char> out(ofs);
copy (in, eos, out);
------------------------------------------------
### CPP-19: std::istreambuf_iterator, std::ostreambuf_iterator and std::transform
------------------------------------------------
ifs >> noskipws;
istreambuf_iterator<char> in(ifs), eos;
ostreambuf_iterator<char> out(ofs);
transform(in, eos, out, char_identity());
------------------------------------------------
### CPP-20: std::istreambuf_iterator and std::string
------------------------------------------------
ifs >> noskipws;
istreambuf_iterator<char> in(ifs), eos;
ofs << string(in, eos);
------------------------------------------------
### CPP-21: std::vector and std::copy
------------------------------------------------
vector<char> v (no_of_file_bytes);
ifs.read(&v[0], no_of_file_bytes);
ostream_iterator<char> out(ofs);
copy (&v[0], &v[v.size()], out);
------------------------------------------------
### CPP-22: std::vector and push_back()
------------------------------------------------
vector<char> v;
v.reserve(no_of_file_bytes);
while (ifs.get(ch)) v.push_back(ch);
ofs << (v.empty() ? string() : string (v.begin(), v.end()));
------------------------------------------------
### CPP-23: std::vector and istream::read()
------------------------------------------------
vector<char> v (no_of_file_bytes);
ifs.read(&v[0], no_of_file_bytes);
ofs << (v.empty() ? string() : string (v.begin(), v.end()));
------------------------------------------------
### CPP-24: std::string and istream::read()
------------------------------------------------
string tmp (no_of_file_bytes, '0');
ifs.read(&tmp[0], no_of_file_bytes);
ofs << tmp;
------------------------------------------------
===================== 4. Methods of copying : END =======================