Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[C++] Simple C/C++ Perfometer : Copying Files

2 views
Skip to first unread message

Alex Vinokur

unread,
Apr 2, 2004, 1:13:14 PM4/2/04
to
// =====================================
// C/C++ Program Performance Measurement
// -------------------------------------
#define PROGRAM_NAME "Simple C/C++ Perfometer : Copying files"
#define PROGRAM_VERSION "Version CF-1.0"
// -------------------------------------
// Copyright (C) 2002-2004 Alex Vinokur
// mailto:ale...@connect.to
// http://up.to/alexv
// =====================================

#include <ctime>
#include <cassert>
#include <string>
#include <vector>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <algorithm>
using namespace std;

// --------------------------------------
#define MAX_VALUE(x,y) ((x) > (y) ? (x) : (y))

// --------------------------------------
string get_compiler_info()
{
ostringstream oss;
string str;
ostringstream tss;


// ------ GNU gcc ------
#ifdef __GNUC__
oss << "GNU gcc version " << __GNUC__;
#ifdef __GNUC_MINOR__
oss << "." << __GNUC_MINOR__;
#ifdef __GNUC_PATCHLEVEL__
#if (__GNUC_PATCHLEVEL__)
oss << "." << __GNUC_PATCHLEVEL__;
#endif
#endif
#endif

#if (__CYGWIN32__ || __CYGWIN__)
oss << " (CYGWIN)";
#endif

#if (__MINGW32__ || __MINGW__ )
oss << " (MINGW)";
#endif

#if (__DJGPP__)
oss << " (DGGPP " << __DJGPP__;
#ifdef __DJGPP_MINOR__
oss << "." << __DJGPP_MINOR__;
#endif
oss << ")";
#endif

#endif

// ------ Microsoft C++ ------
#ifdef _MSC_VER
oss << "Microsoft C++ ";
tss << _MSC_VER;
str = tss.str();
assert (str.size() == 4);
oss << str[0] << str[1] << "." << str[2] << str[3];

#ifdef _MANAGED

#if (_MANAGED)
oss << " (Managed)";
#else
assert (0);
oss << " (Unmanaged)";
#endif
#else

oss << " (Unmanaged)";
#endif

#endif

// ------ Borland C++ ------
#ifdef __BCPLUSPLUS__
oss << "Borland C++ ";
tss << hex << __BCPLUSPLUS__;
str = tss.str();
assert (str.size() == 3);
oss << str[0] << "." << str[1] << "." << str[2];
#endif


// ------ Digital Mars C++ ------
#ifdef __DMC__
#ifndef __DMC_VERSION_STRING__
#error __DMC_VERSION_STRING__ Not Defined
#endif
oss << __DMC_VERSION_STRING__;
#endif

return oss.str();

} // get_compiler_info


// --------------------------------------
void show_compiler_info()
{
const string str(get_compiler_info());

if (str.empty()) return;

cout << string (str.size(), '-') << endl;
cout << str << endl;
cout << string (str.size(), '-') << endl;
}

// --------------------------------------
typedef unsigned long ulong;
typedef unsigned int uint;


// --------------------------------------
static vector<string> foo_names;
static vector<string>::iterator iter_names;
static vector<vector<clock_t> > used_time;

static ulong test_no = 0;
static ulong file_size;
static string in_file_content;

static uint foo_setw = 0;

// ------------------------------
#define INPUT_FILE_NAME "test_i2o.in"
#define OUTPUT_FILE_NAME "test_i2o.out"

static ifstream fs_in;
static ofstream fs_out;

static FILE* fp_in;
static FILE* fp_out;

#define BUFFER_SIZE 4096

static char* mbuffer;


// ==================================
string file_to_string (ifstream& fin_i)
// ==================================
{
assert (fin_i);
assert (fin_i.is_open());

// ---------------------
ios::iostate prev_state = fin_i.rdstate();
ulong prev_pos = fin_i.tellg();

fin_i.clear();

ulong start_pos, end_pos, file_supersize;
fin_i.seekg(0, ios::beg);
start_pos = fin_i.tellg();
fin_i.seekg(0, ios::end);
end_pos = fin_i.tellg();

file_supersize = end_pos - start_pos;
string ret_str (file_supersize, '0');

// ---------------------
fin_i.seekg(0, ios::beg);

char ch;

ulong i;
for (i = 0; fin_i.get (ch); i++)
{
assert (i < ret_str.size());
ret_str[i] = ch;
}

assert (i <= file_supersize);
ret_str.erase (i);

// ---------------------
fin_i.seekg(prev_pos, ios::beg);
fin_i.clear(prev_state);

// ---------------------
return ret_str;

} // file_to_string (1)


// ==================================
string file_to_string (const string& filename_i)
// ==================================
{
ifstream fin (filename_i.c_str());
assert (fin);
assert (fin.is_open());

const string ret_str (file_to_string (fin));

fin.close();
assert (!fin.is_open());

return ret_str;

} // file_to_string (2)


// ------------------------------
void fill_input_file ()
{
ofstream test_infile (INPUT_FILE_NAME);
assert (test_infile);
assert (test_infile.is_open());

char ch = 0;
for (ulong i = 0; i < file_size; i++)
{
while (!isprint(ch)) ch++;
test_infile << ch++;
if (ch == SCHAR_MAX) ch = 0;
}

test_infile.close();
assert (!test_infile.is_open());

in_file_content = file_to_string (INPUT_FILE_NAME);
}


// --------------------------------------
void c_1__functions_getc_putc ()
{
int int_ch;

clearerr(fp_in);
rewind (fp_in);

clearerr(fp_out);
rewind (fp_out);

// ------ Body ------
while ((int_ch = getc(fp_in)) != EOF) putc(int_ch, fp_out);
// ------------------
}

// --------------------------------------
void c_2__functions_fgetc_fputc ()
{
int int_ch;

clearerr(fp_in);
rewind (fp_in);

clearerr(fp_out);
rewind (fp_out);

// ------ Body ------
while ((int_ch = fgetc(fp_in)) != EOF) fputc(int_ch, fp_out);
// ------------------
}

// --------------------------------------
void c_3__functions_fread_fwrite ()
{
char cbuffer[BUFFER_SIZE];
size_t nread;

clearerr(fp_in);
rewind (fp_in);

clearerr(fp_out);
rewind (fp_out);

// ------ Body ------
while ((nread = fread(cbuffer, sizeof(char), sizeof(cbuffer), fp_in)) > 0)
{
fwrite(cbuffer, sizeof(char), nread, fp_out);
}
}

// --------------------------------------
void cpp_1__operators_in_out ()
{
char ch;

fs_in.clear();
fs_in.seekg (0, ios::beg);

fs_out.clear();
fs_out.seekp (0, ios::beg);

// ------ Body ------
fs_in.unsetf(ios::skipws);
while (fs_in >> ch) fs_out << ch;
// ------------------

}

// --------------------------------------
void cpp_2__methods_get_put ()
{
char ch;

fs_in.clear();
fs_in.seekg (0, ios::beg);

fs_out.clear();
fs_out.seekp (0, ios::beg);

// ------ Body ------
while (fs_in.get(ch)) fs_out.put(ch);
// ------------------
}

// --------------------------------------
void cpp_3__methods_sbumpc_sputc ()
{
char ch;

fs_in.clear();
fs_in.seekg (0, ios::beg);

fs_out.clear();
fs_out.seekp (0, ios::beg);

// ------ Body ------
while ((ch = fs_in.rdbuf()->sbumpc()) != EOF) fs_out.rdbuf()->sputc(ch);
// ------------------
}

// --------------------------------------
void cpp_4__method_sbumpc__op_out ()
{
char ch;

fs_in.clear();
fs_in.seekg (0, ios::beg);

fs_out.clear();
fs_out.seekp (0, ios::beg);

// ------ Body ------
ch = fs_in.rdbuf()->sbumpc();
fs_out << ch;

while (ch != EOF)
{
fs_out << fs_in.rdbuf();
ch = fs_in.rdbuf()->sbumpc();
}

}

// --------------------------------------
void cpp_5__method_rdbuf__op_out ()
{
fs_in.clear();
fs_in.seekg (0, ios::beg);

fs_out.clear();
fs_out.seekp (0, ios::beg);

// ------ Body ------
fs_out << fs_in.rdbuf();
// ------------------

}

// --------------------------------------
void cpp_6__methods_cpp_read_write__const_buf ()
{
char cbuffer[BUFFER_SIZE];

fs_in.clear();
fs_in.seekg (0, ios::beg);

fs_out.clear();
fs_out.seekp (0, ios::beg);

// ------ Body ------
while (!fs_in.eof())
{
fs_in.read (cbuffer, sizeof(cbuffer));
fs_out.write (cbuffer, fs_in.gcount());
}
// ------------------
}

// --------------------------------------
void cpp_7__methods_cpp_read_write__max_buf ()
{
fs_in.clear();
fs_in.seekg (0, ios::beg);

fs_out.clear();
fs_out.seekp (0, ios::beg);

// ------ Body ------
fs_in.read (mbuffer, file_size);
fs_out.write (mbuffer, file_size);
// ------------------

}


// -----------------------------------
// -----------------------------------
// -----------------------------------
#define MEASURE_IT(x, y) \
foo_setw = MAX_VALUE (foo_setw, string (#y).size()); \
start_time = clock(); \
assert (start_time != clock_t (-1)); \
for (ulong k = 0; k < no_of_repetitions; k++) x;\
end_time = clock(); \
assert (end_time != clock_t (-1)); \
if (!(end_time > start_time)) { cout << "Number of repetitions is too small" << endl << "Good Luck" << endl << endl; exit(1); }\
assert (end_time > start_time); \
if (find (foo_names.begin(), foo_names.end(), #y) == foo_names.end()) \
{ \
foo_names.push_back (#y); \
used_time.push_back (vector<clock_t>()); \
} \
assert (foo_names.size() == used_time.size()); \
iter_names = find (foo_names.begin(), foo_names.end(), #y); \
assert (iter_names != foo_names.end()); \
used_time[distance (foo_names.begin(), iter_names)].push_back ((end_time - start_time))


#define MEASURE_WITH_ARG(foo, argument) MEASURE_IT (foo(argument), foo)
#define MEASURE_WITH_NO_ARG(foo) MEASURE_IT (foo(), foo)


// ------------
void measure (ulong no_of_repetitions)
{
clock_t start_time;
clock_t end_time;
vector<clock_t> elapsed_time_vect;

// -------------------------------
fill_input_file ();
mbuffer = new char [file_size];

fp_in = fopen(INPUT_FILE_NAME, "r");
assert(fp_in);

fp_out = fopen(OUTPUT_FILE_NAME, "w");
assert(fp_out);

fs_in.open (INPUT_FILE_NAME);
assert (fs_in);
assert (fs_in.is_open());

fs_out.open (OUTPUT_FILE_NAME);
assert (fs_out);
assert (fs_out.is_open());

assert (in_file_content.size() == file_size);


// -------------------------------
// cout << "\t---> Test-" << ++test_no << " started" << endl;
cout << ".";
cout.flush();


// -------------------------------
MEASURE_WITH_NO_ARG (c_1__functions_getc_putc);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (c_2__functions_fgetc_fputc);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (c_3__functions_fread_fwrite);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_1__operators_in_out);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_2__methods_get_put);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_3__methods_sbumpc_sputc);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_4__method_sbumpc__op_out);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_5__method_rdbuf__op_out);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_6__methods_cpp_read_write__const_buf);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_7__methods_cpp_read_write__max_buf);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));


// -------------------------------
// cerr << "\t Test-" << test_no << " finished" << endl;


// -------------------------------
fs_in.clear();
fs_in.close();
assert (!fs_in.is_open());

fs_out.close();
assert (!fs_out.is_open());


clearerr(fp_in);
fclose(fp_in);

clearerr(fp_out);
fclose(fp_out);

}


// ------------
void show (ulong no_of_tests)
{
clock_t units;
clock_t sum;
#define THRESHOLD 0.2
const ulong threshold = ulong(no_of_tests * THRESHOLD);

assert ((threshold * 2) <= no_of_tests);

cout << endl;
assert (foo_names.size() == used_time.size());

for (ulong i = 0; i < foo_names.size(); i++)
{
sum = 0;
assert (no_of_tests == used_time[i].size());
for (ulong k = threshold; k < (used_time[i].size() - threshold); k++)
{
sum += used_time[i][k];
}
units = sum/(used_time[i].size() - (threshold * 2));


cout << setw(foo_setw)
<< std::left
<< foo_names[i]
<< " : "
<< setw(6)
<< std::right
<< units
<< " units"
<< " (";
cout.setf(ios::fixed, ios::floatfield);
cout << setprecision (3)
<< (float(units)/float(CLOCKS_PER_SEC))
<< " secs)"
<< endl;
}

}

// ------------
void run (ulong no_of_runs, ulong no_of_tests, ulong no_of_repetitions)
{
for (ulong i = 0; i < no_of_runs; i++)
{
test_no = 0;
foo_names.clear();
used_time.clear();

// ----------------------
cout << endl << endl << " Run-" << (i + 1) << " of " << no_of_runs << " : Started ";
cout.flush();
for (ulong k = 0; k < no_of_tests; k++) measure (no_of_repetitions);
show (no_of_tests);
cout << " Run-" << (i + 1) << " of " << no_of_runs << " : Finished"<< endl << endl;
}
}


// ------------
int main(int argc, char** argv)
{

cout << endl;
cout << string (string (PROGRAM_NAME).size(), '=') << endl;
cout << PROGRAM_NAME << endl;
cout << PROGRAM_VERSION << endl;
cout << string (string (PROGRAM_NAME).size(), '=') << endl;
// --------------------------

cout << endl;
cout << endl;
show_compiler_info();

cout << endl;
cout << "\tYOUR COMMAND LINE : ";
for (long i = 0; i < argc; i++) cout << argv[i] << " ";
cout << endl;
cout << endl;

if (!(argc >= 4))
{
cout << "\tUSAGE : "
<< argv[0]
<< " "
<< "<File size> <No. of tests> <No. of repetitions> [<No. of runs>]"
<< endl;
return 1;
}
assert (argc >= 4);

file_size = atoi (argv[1]);
assert (file_size > 0);

const ulong no_of_tests = atoi (argv[2]);
assert (no_of_tests > 0);

const ulong no_of_repetitions = atoi (argv[3]);
assert (no_of_repetitions > 0);

const ulong no_of_runs = ((argc > 4) ? atoi (argv[4]) : 1);
assert (no_of_runs > 0);

cout << "\t### File size : " << file_size << endl;
cout << "\t### Number of runs : " << no_of_runs << endl;
cout << "\t### Number of tests : " << no_of_tests << endl;
cout << "\t### Number of repetitions : " << no_of_repetitions << endl;
cout << "\t### CLOCKS_PER_SEC : " << CLOCKS_PER_SEC << endl;
cout << endl;


// -----------------------------
run (no_of_runs, no_of_tests, no_of_repetitions);

return 0;
}

Alex Vinokur

unread,
Apr 18, 2004, 8:34:14 AM4/18/04
to
// =====================================
// C/C++ Program Performance Measurement
// -------------------------------------
#define PROGRAM_NAME "Simple C/C++ Perfometer : Copying files"
#define PROGRAM_VERSION "Version CF-1.2"

// -------------------------------------
// Copyright (C) 2002-2004 Alex Vinokur
// mailto:ale...@connect.to
// http://up.to/alexv
// =====================================

#include <ctime>
#include <cassert>
#include <string>
#include <vector>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <algorithm>
using namespace std;


// --------------------------------------


typedef unsigned long ulong;
typedef unsigned int uint;

// --------------------------------------


#define MAX_VALUE(x,y) ((x) > (y) ? (x) : (y))

// --------------------------------------
string get_compiler_info()
{
ostringstream oss;

ostringstream tss;
string str;


// ------ GNU gcc ------
#ifdef __GNUC__

oss.str("");
tss.str("");
str.erase();

oss << "GNU gcc " << __GNUC__;


#ifdef __GNUC_MINOR__
oss << "." << __GNUC_MINOR__;
#ifdef __GNUC_PATCHLEVEL__
#if (__GNUC_PATCHLEVEL__)
oss << "." << __GNUC_PATCHLEVEL__;
#endif
#endif
#endif

#if (__CYGWIN32__ || __CYGWIN__)
oss << " (CYGWIN)";
#endif

#if (__MINGW32__ || __MINGW__ )
oss << " (MINGW)";
#endif

#if (__DJGPP__)
oss << " (DGGPP " << __DJGPP__;
#ifdef __DJGPP_MINOR__
oss << "." << __DJGPP_MINOR__;
#endif
oss << ")";
#endif

#endif

// ------ Microsoft C++ ------
#ifdef _MSC_VER

oss.str("");
tss.str("");
str.erase();

oss << "Microsoft C++ ";
tss << _MSC_VER;
str = tss.str();
assert (str.size() == 4);
oss << str[0] << str[1] << "." << str[2] << str[3];

#ifdef _MANAGED

#if (_MANAGED)
oss << " (Managed)";
#else
assert (0);
oss << " (Unmanaged)";
#endif
#else

oss << " (Unmanaged)";
#endif

#endif

// ------ Intel C++ ------
#ifdef __INTEL_COMPILER
oss.str("");
tss.str("");
str.erase();

oss << "Intel C++ ";
tss << __INTEL_COMPILER;


str = tss.str();
assert (str.size() == 3);

oss << str[0] << "." << str[1];
#endif


// ------ Borland C++ ------
#ifdef __BCPLUSPLUS__

oss.str("");
tss.str("");
str.erase();

oss << "Borland C++ ";
tss << hex << __BCPLUSPLUS__;
str = tss.str();
assert (str.size() == 3);
oss << str[0] << "." << str[1] << "." << str[2];
#endif


// ------ Digital Mars C++ ------
#ifdef __DMC__

oss.str("");
tss.str("");
str.erase();

#ifndef __DMC_VERSION_STRING__
#error __DMC_VERSION_STRING__ Not Defined
#endif
oss << __DMC_VERSION_STRING__;
#endif

return oss.str();

} // get_compiler_info


// --------------------------------------
void show_compiler_info()
{
const string str(get_compiler_info());

if (str.empty()) return;

cout << string (str.size(), '-') << endl;
cout << str << endl;
cout << string (str.size(), '-') << endl;
}

// --------------------------------------


static vector<string> foo_names;
static vector<string>::iterator iter_names;
static vector<vector<clock_t> > used_time;

static ulong test_no = 0;
static ulong file_size;
static string in_file_content;

static uint foo_setw = 0;

// ------------------------------
#define INPUT_FILE_NAME "test_i2o.in"
#define OUTPUT_FILE_NAME "test_i2o.out"

#define ROWS_IN_INPUT_FILE 7

#define BUFFER_SIZE 4096

static char* mbuffer;

fin_i.clear();

char ch;

// ---------------------
return ret_str;

} // file_to_string (1)

fin.close();
assert (!fin.is_open());

return ret_str;

} // file_to_string (2)


// ------------------------------
void fill_input_file (uint total_full_rows = ROWS_IN_INPUT_FILE)
{
assert (total_full_rows > 0);
assert (total_full_rows < file_size);

ofstream test_infile (INPUT_FILE_NAME);
assert (test_infile);
assert (test_infile.is_open());

const uint row_size = file_size/total_full_rows;

char ch = 0;

ulong counter = 0;
for (ulong row_no = 0; row_no < total_full_rows; row_no++)
{
for (ulong ch_no = 0; ch_no < row_size; ch_no++)
{
while (!isprint(ch)) ch++;
assert (counter < file_size);

test_infile << ch++;
counter++;
if (counter == file_size) break;

if (ch == SCHAR_MAX) ch = 0;
}

if (counter == file_size) break;
assert (counter < file_size);

test_infile << '\n';
counter++;
if (counter == file_size) break;
}

/*


for (ulong i = 0; i < file_size; i++)
{
while (!isprint(ch)) ch++;
test_infile << ch++;
if (ch == SCHAR_MAX) ch = 0;
}

*/

test_infile.close();
assert (!test_infile.is_open());

in_file_content = file_to_string (INPUT_FILE_NAME);
}


// --------------------------------------
void c_01__functions_getc_putc ()
{
int int_ch;

clearerr(fp_in);
rewind (fp_in);

clearerr(fp_out);
rewind (fp_out);

// ------ Body ------
while ((int_ch = getc(fp_in)) != EOF) putc(int_ch, fp_out);
// ------------------
}

// --------------------------------------
void c_02__functions_fgetc_fputc ()
{
int int_ch;

clearerr(fp_in);
rewind (fp_in);

clearerr(fp_out);
rewind (fp_out);

// ------ Body ------
while ((int_ch = fgetc(fp_in)) != EOF) fputc(int_ch, fp_out);
// ------------------
}

// --------------------------------------
void c_03__functions_fread_fwrite ()


{
char cbuffer[BUFFER_SIZE];
size_t nread;

clearerr(fp_in);
rewind (fp_in);

clearerr(fp_out);
rewind (fp_out);

// ------ Body ------
while ((nread = fread(cbuffer, sizeof(char), sizeof(cbuffer), fp_in)) > 0)
{
fwrite(cbuffer, sizeof(char), nread, fp_out);
}
}

// --------------------------------------
void cpp_01__operators_in_out ()
{
char ch;

fs_in.clear();
fs_in.seekg (0, ios::beg);

fs_out.clear();
fs_out.seekp (0, ios::beg);

// ------ Body ------
fs_in.unsetf(ios::skipws);
while (fs_in >> ch) fs_out << ch;
// ------------------

}

// --------------------------------------
void cpp_02__methods_get_put ()
{
char ch;

fs_in.clear();
fs_in.seekg (0, ios::beg);

fs_out.clear();
fs_out.seekp (0, ios::beg);

// ------ Body ------
while (fs_in.get(ch)) fs_out.put(ch);
// ------------------
}

// --------------------------------------
void cpp_03__methods_sbumpc_sputc ()
{
char ch;

fs_in.clear();
fs_in.seekg (0, ios::beg);

fs_out.clear();
fs_out.seekp (0, ios::beg);

// ------ Body ------
while ((ch = fs_in.rdbuf()->sbumpc()) != EOF) fs_out.rdbuf()->sputc(ch);
// ------------------
}

// --------------------------------------
void cpp_04__method_sbumpc__op_out ()
{
char ch;

fs_in.clear();
fs_in.seekg (0, ios::beg);

fs_out.clear();
fs_out.seekp (0, ios::beg);

// ------ Body ------
ch = fs_in.rdbuf()->sbumpc();
fs_out << ch;

while (ch != EOF)
{
fs_out << fs_in.rdbuf();
ch = fs_in.rdbuf()->sbumpc();
}

}

// --------------------------------------
void cpp_05__method_rdbuf__op_out ()


{
fs_in.clear();
fs_in.seekg (0, ios::beg);

fs_out.clear();
fs_out.seekp (0, ios::beg);

// ------ Body ------
fs_out << fs_in.rdbuf();
// ------------------

}

// --------------------------------------
void cpp_06__methods_cpp_read_write__const_buf ()
{
char cbuffer[BUFFER_SIZE];

fs_in.clear();
fs_in.seekg (0, ios::beg);

fs_out.clear();
fs_out.seekp (0, ios::beg);

// ------ Body ------
while (!fs_in.eof())
{
fs_in.read (cbuffer, sizeof(cbuffer));
fs_out.write (cbuffer, fs_in.gcount());
}
// ------------------
}

// --------------------------------------
void cpp_07__methods_cpp_read_write__max_buf ()


{
fs_in.clear();
fs_in.seekg (0, ios::beg);

fs_out.clear();
fs_out.seekp (0, ios::beg);

// ------ Body ------
fs_in.read (mbuffer, file_size);
fs_out.write (mbuffer, file_size);
// ------------------

}


// --------------------------------------
void cpp_08__method_getline ()
{
string line;

fs_in.clear();
fs_in.seekg (0, ios::beg);

fs_out.clear();
fs_out.seekp (0, ios::beg);

// ------ Body ------
while (getline (fs_in, line)) fs_out << line << '\n';
// ------------------

}

// --------------------------------------
void cpp_09__method_ifstream_getline ()
{
char buffer[BUFFER_SIZE];

fs_in.clear();
fs_in.seekg (0, ios::beg);

fs_out.clear();
fs_out.seekp (0, ios::beg);

// ------ Body ------
while (fs_in.getline (buffer, sizeof(buffer))) fs_out << buffer << '\n';
// ------------------

}

if (in_file_content.size() != file_size)
{
cerr << "Requested file size = " << file_size << endl;
cerr << "Input file size = " << in_file_content.size() << endl;
assert (in_file_content.size() == file_size);
}


// -------------------------------
// cout << "\t---> Test-" << ++test_no << " started" << endl;
cout << ".";
cout.flush();

// -------------------------------
MEASURE_WITH_NO_ARG (c_01__functions_getc_putc);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (c_02__functions_fgetc_fputc);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (c_03__functions_fread_fwrite);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_01__operators_in_out);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_02__methods_get_put);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_03__methods_sbumpc_sputc);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_04__method_sbumpc__op_out);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_05__method_rdbuf__op_out);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_06__methods_cpp_read_write__const_buf);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_07__methods_cpp_read_write__max_buf);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));


string str;

MEASURE_WITH_NO_ARG (cpp_08__method_getline);
str = file_to_string(OUTPUT_FILE_NAME);
assert ((in_file_content == str) || (in_file_content == str.substr(0, str.size() - 1)));

MEASURE_WITH_NO_ARG (cpp_09__method_ifstream_getline);
str = file_to_string(OUTPUT_FILE_NAME);
assert ((in_file_content == str) || (in_file_content == str.substr(0, str.size() - 1)));

fs_out.close();
assert (!fs_out.is_open());


clearerr(fp_in);
fclose(fp_in);

clearerr(fp_out);
fclose(fp_out);

}

}

string exe_name (argv[0]);
cout << exe_name.substr (exe_name.find_last_of ("/\\") + 1) << " ";
for (long i = 1; i < argc; i++) cout << argv[i] << " ";

Alex Vinokur

unread,
Apr 19, 2004, 11:19:46 AM4/19/04
to
// =====================================
// C/C++ Program Performance Measurement
// -------------------------------------
#define PROGRAM_NAME "Simple C/C++ Perfometer : Copying files"
#define PROGRAM_VERSION "Version CF-1.3"

// -------------------------------------
// Copyright (C) 2002-2004 Alex Vinokur
// mailto:ale...@connect.to
// http://up.to/alexv
// =====================================

// --------------------------------------
#if ((defined unix) || (defined __unix) || (defined unix__) || (defined __unix__))
#define UNIX_ENV
#endif

// ==============


#include <ctime>
#include <cassert>
#include <string>
#include <vector>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>

#include <iterator>
#include <algorithm>
#ifdef UNIX_ENV
#include <fcntl.h>
#include <sys/mman.h>
#endif
using namespace std;

#endif

#ifdef _MANAGED

oss << " (Unmanaged)";
#endif

#endif

return oss.str();

} // get_compiler_info

if (str.empty()) return;

#define ROWS_IN_INPUT_FILE 7

#ifdef UNIX_ENV
static int fd_in;
static int fd_out;
#endif

#define BUFFER_SIZE 4096

static char* mbuffer;

fin_i.clear();

char ch;

// ---------------------
return ret_str;

} // file_to_string (1)

fin.close();
assert (!fin.is_open());

return ret_str;

} // file_to_string (2)

if (file_size < total_full_rows)
{
total_full_rows = file_size;
}
assert (total_full_rows <= file_size);

char ch = 0;

test_infile.close();
assert (!test_infile.is_open());

in_file_content = file_to_string (INPUT_FILE_NAME);
}

clearerr(fp_in);
rewind (fp_in);

clearerr(fp_out);
rewind (fp_out);

clearerr(fp_in);
rewind (fp_in);

clearerr(fp_out);
rewind (fp_out);

clearerr(fp_in);
rewind (fp_in);

clearerr(fp_out);
rewind (fp_out);


#ifdef UNIX_ENV
// --------------------------------------
void unix_c_04__mmap ()
{
off_t rc;
rc = lseek(fd_in, 0, SEEK_SET);
rc = lseek(fd_out, 0, SEEK_SET);

// ------ Body ------
char* ptr = (char*)mmap(0, file_size, PROT_READ, MAP_SHARED, fd_in, 0);
assert (ptr != MAP_FAILED);
write(fd_out, ptr, file_size);
munmap(ptr, file_size);
// ------------------
}
#endif

}

}

}

}

}

}

// --------------------------------------
void cpp_10__iterators ()
{
char buffer[BUFFER_SIZE];

fs_in.clear();
fs_in.seekg (0, ios::beg);

fs_out.clear();
fs_out.seekp (0, ios::beg);

// ------ Body ------
fs_in >> noskipws;
istream_iterator<char> in(fs_in), eos;
ostream_iterator<char> out(fs_out);
copy (in, eos, out);
// ------------------

}

#ifdef UNIX_ENV
fd_in = open(INPUT_FILE_NAME, O_RDONLY);
assert (fd_in != -1);

fd_out = open(OUTPUT_FILE_NAME, O_WRONLY);
assert (fd_out != -1);

assert (fd_in != fd_out);
#endif

fp_out = fopen(OUTPUT_FILE_NAME, "w");
assert(fp_out);

fs_in.open (INPUT_FILE_NAME);
assert (fs_in);
assert (fs_in.is_open());

fs_out.open (OUTPUT_FILE_NAME);
assert (fs_out);
assert (fs_out.is_open());

if (in_file_content.size() != file_size)
{
cerr << "Requested file size = " << file_size << endl;
cerr << "Input file size = " << in_file_content.size() << endl;
assert (in_file_content.size() == file_size);
}


// -------------------------------
// cout << "\t---> Test-" << ++test_no << " started" << endl;
cout << ".";
cout.flush();

// -------------------------------
MEASURE_WITH_NO_ARG (c_01__functions_getc_putc);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (c_02__functions_fgetc_fputc);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (c_03__functions_fread_fwrite);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

#ifdef UNIX_ENV
MEASURE_WITH_NO_ARG (unix_c_04__mmap);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));
#endif

MEASURE_WITH_NO_ARG (cpp_01__operators_in_out);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_02__methods_get_put);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_03__methods_sbumpc_sputc);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_04__method_sbumpc__op_out);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_05__method_rdbuf__op_out);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_06__methods_cpp_read_write__const_buf);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_07__methods_cpp_read_write__max_buf);
assert (in_file_content == file_to_string(OUTPUT_FILE_NAME));


string str;

MEASURE_WITH_NO_ARG (cpp_08__method_getline);
str = file_to_string(OUTPUT_FILE_NAME);
assert ((in_file_content == str) || (in_file_content == str.substr(0, str.size() - 1)));

MEASURE_WITH_NO_ARG (cpp_09__method_ifstream_getline);
str = file_to_string(OUTPUT_FILE_NAME);
assert ((in_file_content == str) || (in_file_content == str.substr(0, str.size() - 1)));

MEASURE_WITH_NO_ARG (cpp_10__iterators);

fs_out.close();
assert (!fs_out.is_open());


clearerr(fp_in);
fclose(fp_in);

clearerr(fp_out);
fclose(fp_out);

}

}

return 0;
}


--
Alex Vinokur
mailto:ale...@connect.to
http://mathforum.org/library/view/10978.html

Alex Vinokur

unread,
Apr 20, 2004, 5:03:09 AM4/20/04
to
// =====================================
// C/C++ Program Performance Measurement
// -------------------------------------
#define PROGRAM_NAME "Simple C/C++ Perfometer : Copying files"
#define PROGRAM_VERSION "Version CF-1.4"

// -------------------------------------
// Copyright (C) 2002-2004 Alex Vinokur
// mailto:ale...@connect.to
// http://up.to/alexv
// =====================================

// --------------------------------------


#if ((defined unix) || (defined __unix) || (defined unix__) || (defined __unix__))
#define UNIX_ENV
#endif

// ==============


#include <ctime>
#include <cassert>
#include <string>
#include <vector>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>

#include <iterator>
#include <algorithm>
#ifdef UNIX_ENV
#include <fcntl.h>
#include <sys/mman.h>
#endif

using namespace std;


// --------------------------------------


typedef unsigned long ulong;
typedef unsigned int uint;


// --------------------------------------


#define MAX_VALUE(x,y) ((x) > (y) ? (x) : (y))

// --------------------------------------
string get_compiler_info()
{
ostringstream oss;

ostringstream tss;
string str;


// ------ GNU gcc ------
#ifdef __GNUC__

oss.str("");
tss.str("");
str.erase();

oss << "GNU gcc " << __GNUC__;


#ifdef __GNUC_MINOR__
oss << "." << __GNUC_MINOR__;
#ifdef __GNUC_PATCHLEVEL__
#if (__GNUC_PATCHLEVEL__)
oss << "." << __GNUC_PATCHLEVEL__;
#endif
#endif
#endif

#if (__CYGWIN32__ || __CYGWIN__)
oss << " (CYGWIN)";
#endif

#if (__MINGW32__ || __MINGW__ )
oss << " (MINGW)";
#endif

#if (__DJGPP__)
oss << " (DGGPP " << __DJGPP__;
#ifdef __DJGPP_MINOR__
oss << "." << __DJGPP_MINOR__;
#endif
oss << ")";
#endif

#endif

// ------ Microsoft C++ ------
#ifdef _MSC_VER

oss.str("");
tss.str("");
str.erase();

oss << "Microsoft C++ ";


tss << _MSC_VER;
str = tss.str();
assert (str.size() == 4);
oss << str[0] << str[1] << "." << str[2] << str[3];

#ifdef _MANAGED

#if (_MANAGED)
oss << " (Managed)";
#else
assert (0);
oss << " (Unmanaged)";
#endif
#else

oss << " (Unmanaged)";
#endif

#endif

// ------ Intel C++ ------


#ifdef __INTEL_COMPILER
oss.str("");
tss.str("");
str.erase();

oss << "Intel C++ ";
tss << __INTEL_COMPILER;

str = tss.str();
assert (str.size() == 3);

oss << str[0] << "." << str[1];
#endif


// ------ Borland C++ ------
#ifdef __BCPLUSPLUS__

oss.str("");
tss.str("");
str.erase();

oss << "Borland C++ ";


tss << hex << __BCPLUSPLUS__;
str = tss.str();
assert (str.size() == 3);
oss << str[0] << "." << str[1] << "." << str[2];
#endif


// ------ Digital Mars C++ ------
#ifdef __DMC__

oss.str("");
tss.str("");
str.erase();

#ifndef __DMC_VERSION_STRING__


#error __DMC_VERSION_STRING__ Not Defined
#endif
oss << __DMC_VERSION_STRING__;
#endif

return oss.str();

} // get_compiler_info


// --------------------------------------
void show_compiler_info()
{
const string str(get_compiler_info());

if (str.empty()) return;

cout << string (str.size(), '-') << endl;
cout << str << endl;
cout << string (str.size(), '-') << endl;
}

// --------------------------------------


static vector<string> foo_names;
static vector<string>::iterator iter_names;
static vector<vector<clock_t> > used_time;

static ulong test_no = 0;
static ulong file_size;
static string in_file_content;

static string in_file_tail_delta;

static uint foo_setw = 0;

// ------------------------------
#define INPUT_TXT_FILE_NAME "z-txt.in"
#define OUTPUT_TXT_FILE_NAME "z-txt.out"
#define INPUT_BIN_FILE_NAME "z-bin.in"
#define OUTPUT_BIN_FILE_NAME "z-bin.out"


#define ROWS_IN_INPUT_FILE 7

static FILE* fp_txt_in;
static FILE* fp_txt_out;
static FILE* fp_bin_in;
static FILE* fp_bin_out;

#ifdef UNIX_ENV
static int fd_txt_in;
static int fd_txt_out;
static int fd_bin_in;
static int fd_bin_out;
#endif

static ifstream fs_txt_in;
static ofstream fs_txt_out;
static ifstream fs_bin_in;
static ofstream fs_bin_out;


#define BUFFER_SIZE 4096

static char* mbuffer;

fin_i.clear();

char ch;

// ---------------------
return ret_str;

} // file_to_string (1)

fin.close();
assert (!fin.is_open());

return ret_str;

} // file_to_string (2)


// ------------------------------


void fill_input_file (uint total_full_rows = ROWS_IN_INPUT_FILE)
{
assert (total_full_rows > 0);
if (file_size < total_full_rows)
{
total_full_rows = file_size;
}
assert (total_full_rows <= file_size);

ofstream test_txt_infile (INPUT_TXT_FILE_NAME);
assert (test_txt_infile);
assert (test_txt_infile.is_open());

ofstream test_bin_infile (INPUT_BIN_FILE_NAME);
assert (test_bin_infile);
assert (test_bin_infile.is_open());


const uint row_size = file_size/total_full_rows;

char ch = 0;

ulong counter = 0;
for (ulong row_no = 0; row_no < total_full_rows; row_no++)
{
for (ulong ch_no = 0; ch_no < row_size; ch_no++)
{
while (!isprint(ch)) ch++;
assert (counter < file_size);

test_txt_infile << ch;
test_bin_infile << ch;


ch++;
counter++;
if (counter == file_size) break;

if (ch == SCHAR_MAX) ch = 0;
}

if (counter == file_size) break;
assert (counter < file_size);

test_txt_infile << '\n';
test_bin_infile << '\n';


counter++;
if (counter == file_size) break;
}

/*


for (ulong i = 0; i < file_size; i++)
{
while (!isprint(ch)) ch++;

test_txt_infile << ch++;


if (ch == SCHAR_MAX) ch = 0;
}

*/

test_txt_infile.close();
assert (!test_txt_infile.is_open());

test_bin_infile.close();
assert (!test_bin_infile.is_open());

const string str_txt (file_to_string (INPUT_TXT_FILE_NAME));
const string str_bin (file_to_string (INPUT_BIN_FILE_NAME));
assert (str_txt == str_bin);

const string tail_txt (((*str_txt.rbegin()) == '\n') ? "" : "\n");
const string tail_bin (((*str_bin.rbegin()) == '\n') ? "" : "\n");
assert (tail_txt == tail_bin);

in_file_content = str_txt;
in_file_tail_delta = tail_txt;

}


// --------------------------------------
void C_01_txt__functions_getc_putc ()
{
int int_ch;

clearerr(fp_txt_in);
rewind (fp_txt_in);

clearerr(fp_txt_out);
rewind (fp_txt_out);

// ------ Body ------
while ((int_ch = getc(fp_txt_in)) != EOF) putc(int_ch, fp_txt_out);
// ------------------
}

// --------------------------------------
void C_01_bin__functions_getc_putc ()
{
int int_ch;

clearerr(fp_bin_in);
rewind (fp_bin_in);

clearerr(fp_bin_out);
rewind (fp_bin_out);

// ------ Body ------
while ((int_ch = getc(fp_bin_in)) != EOF) putc(int_ch, fp_bin_out);
// ------------------
}


// --------------------------------------
void C_02_txt__functions_fgetc_fputc ()
{
int int_ch;

clearerr(fp_txt_in);
rewind (fp_txt_in);

clearerr(fp_txt_out);
rewind (fp_txt_out);

// ------ Body ------
while ((int_ch = fgetc(fp_txt_in)) != EOF) fputc(int_ch, fp_txt_out);
// ------------------
}


// --------------------------------------
void C_02_bin__functions_fgetc_fputc ()
{
int int_ch;

clearerr(fp_bin_in);
rewind (fp_bin_in);

clearerr(fp_bin_out);
rewind (fp_bin_out);

// ------ Body ------
while ((int_ch = fgetc(fp_bin_in)) != EOF) fputc(int_ch, fp_bin_out);
// ------------------
}


// --------------------------------------
void C_03_txt__functions_fread_fwrite ()


{
char cbuffer[BUFFER_SIZE];
size_t nread;

clearerr(fp_txt_in);
rewind (fp_txt_in);

clearerr(fp_txt_out);
rewind (fp_txt_out);

// ------ Body ------
while ((nread = fread(cbuffer, sizeof(char), sizeof(cbuffer), fp_txt_in)) > 0)
{
fwrite(cbuffer, sizeof(char), nread, fp_txt_out);
}
// ------------------
}


// --------------------------------------
void C_03_bin__functions_fread_fwrite ()


{
char cbuffer[BUFFER_SIZE];
size_t nread;

clearerr(fp_bin_in);
rewind (fp_bin_in);

clearerr(fp_bin_out);
rewind (fp_bin_out);

// ------ Body ------
while ((nread = fread(cbuffer, sizeof(char), sizeof(cbuffer), fp_bin_in)) > 0)
{
fwrite(cbuffer, sizeof(char), nread, fp_bin_out);
}
// ------------------
}

#ifdef UNIX_ENV
// --------------------------------------
void unix_C_04_txt__mmap ()
{
off_t rc;
rc = lseek(fd_txt_in, 0, SEEK_SET);
rc = lseek(fd_txt_out, 0, SEEK_SET);

// ------ Body ------
char* ptr = (char*)mmap(0, file_size, PROT_READ, MAP_SHARED, fd_txt_in, 0);
assert (ptr != MAP_FAILED);
write(fd_txt_out, ptr, file_size);


munmap(ptr, file_size);
// ------------------
}
#endif


// --------------------------------------
void cpp_01_txt__operators_in_out ()
{
char ch;

fs_txt_in.clear();
fs_txt_in.seekg (0, ios::beg);

fs_txt_out.clear();
fs_txt_out.seekp (0, ios::beg);

// ------ Body ------
fs_txt_in.unsetf(ios::skipws);
while (fs_txt_in >> ch) fs_txt_out << ch;
// ------------------
}


// --------------------------------------
void cpp_01_bin__operators_in_out ()
{
char ch;

fs_bin_in.clear();
fs_bin_in.seekg (0, ios::beg);

fs_bin_out.clear();
fs_bin_out.seekp (0, ios::beg);

// ------ Body ------
fs_bin_in.unsetf(ios::skipws);
while (fs_bin_in >> ch) fs_bin_out << ch;
// ------------------
}


// --------------------------------------
void cpp_02_txt__methods_get_put ()
{
char ch;

fs_txt_in.clear();
fs_txt_in.seekg (0, ios::beg);

fs_txt_out.clear();
fs_txt_out.seekp (0, ios::beg);

// ------ Body ------
while (fs_txt_in.get(ch)) fs_txt_out.put(ch);
// ------------------
}


// --------------------------------------
void cpp_02_bin__methods_get_put ()
{
char ch;

fs_bin_in.clear();
fs_bin_in.seekg (0, ios::beg);

fs_bin_out.clear();
fs_bin_out.seekp (0, ios::beg);

// ------ Body ------
while (fs_bin_in.get(ch)) fs_bin_out.put(ch);
// ------------------
}


// --------------------------------------
void cpp_03_txt__methods_sbumpc_sputc ()
{
char ch;

fs_txt_in.clear();
fs_txt_in.seekg (0, ios::beg);

fs_txt_out.clear();
fs_txt_out.seekp (0, ios::beg);

// ------ Body ------
while ((ch = fs_txt_in.rdbuf()->sbumpc()) != EOF) fs_txt_out.rdbuf()->sputc(ch);
// ------------------
}


// --------------------------------------
void cpp_03_bin__methods_sbumpc_sputc ()
{
char ch;

fs_bin_in.clear();
fs_bin_in.seekg (0, ios::beg);

fs_bin_out.clear();
fs_bin_out.seekp (0, ios::beg);

// ------ Body ------
while ((ch = fs_bin_in.rdbuf()->sbumpc()) != EOF) fs_bin_out.rdbuf()->sputc(ch);
// ------------------
}


// --------------------------------------
void cpp_04_txt__method_sbumpc__op_out ()
{
char ch;

fs_txt_in.clear();
fs_txt_in.seekg (0, ios::beg);

fs_txt_out.clear();
fs_txt_out.seekp (0, ios::beg);

// ------ Body ------
ch = fs_txt_in.rdbuf()->sbumpc();
fs_txt_out << ch;

while (ch != EOF)
{
fs_txt_out << fs_txt_in.rdbuf();
ch = fs_txt_in.rdbuf()->sbumpc();
}

}

// --------------------------------------
void cpp_04_bin__method_sbumpc__op_out ()
{
char ch;

fs_bin_in.clear();
fs_bin_in.seekg (0, ios::beg);

fs_bin_out.clear();
fs_bin_out.seekp (0, ios::beg);

// ------ Body ------
ch = fs_bin_in.rdbuf()->sbumpc();
fs_bin_out << ch;

while (ch != EOF)
{
fs_bin_out << fs_bin_in.rdbuf();
ch = fs_bin_in.rdbuf()->sbumpc();
}

}


// --------------------------------------
void cpp_05_txt__method_rdbuf__op_out ()
{
fs_txt_in.clear();
fs_txt_in.seekg (0, ios::beg);

fs_txt_out.clear();
fs_txt_out.seekp (0, ios::beg);

// ------ Body ------
fs_txt_out << fs_txt_in.rdbuf();
// ------------------

}


// --------------------------------------
void cpp_05_bin__method_rdbuf__op_out ()
{
fs_bin_in.clear();
fs_bin_in.seekg (0, ios::beg);

fs_bin_out.clear();
fs_bin_out.seekp (0, ios::beg);

// ------ Body ------
fs_bin_out << fs_bin_in.rdbuf();
// ------------------

}


// --------------------------------------
void cpp_06_txt__methods_cpp_read_write__const_buf ()
{
char cbuffer[BUFFER_SIZE];

fs_txt_in.clear();
fs_txt_in.seekg (0, ios::beg);

fs_txt_out.clear();
fs_txt_out.seekp (0, ios::beg);

// ------ Body ------
while (!fs_txt_in.eof())
{
fs_txt_in.read (cbuffer, sizeof(cbuffer));
fs_txt_out.write (cbuffer, fs_txt_in.gcount());
}
// ------------------
}


// --------------------------------------
void cpp_06_bin__methods_cpp_read_write__const_buf ()
{
char cbuffer[BUFFER_SIZE];

fs_bin_in.clear();
fs_bin_in.seekg (0, ios::beg);

fs_bin_out.clear();
fs_bin_out.seekp (0, ios::beg);

// ------ Body ------
while (!fs_bin_in.eof())
{
fs_bin_in.read (cbuffer, sizeof(cbuffer));
fs_bin_out.write (cbuffer, fs_bin_in.gcount());
}
// ------------------
}


// --------------------------------------
void cpp_07_txt__methods_cpp_read_write__max_buf ()
{
fs_txt_in.clear();
fs_txt_in.seekg (0, ios::beg);

fs_txt_out.clear();
fs_txt_out.seekp (0, ios::beg);

// ------ Body ------
fs_txt_in.read (mbuffer, file_size);
fs_txt_out.write (mbuffer, file_size);
// ------------------

}

// --------------------------------------
void cpp_07_bin__methods_cpp_read_write__max_buf ()
{
fs_bin_in.clear();
fs_bin_in.seekg (0, ios::beg);

fs_bin_out.clear();
fs_bin_out.seekp (0, ios::beg);

// ------ Body ------
fs_bin_in.read (mbuffer, file_size);
fs_bin_out.write (mbuffer, file_size);
// ------------------

}

// --------------------------------------
void cpp_08_txt__method_getline ()
{
string line;

fs_txt_in.clear();
fs_txt_in.seekg (0, ios::beg);

fs_txt_out.clear();
fs_txt_out.seekp (0, ios::beg);

// ------ Body ------
while (getline (fs_txt_in, line)) fs_txt_out << line << '\n';
// ------------------

}

// --------------------------------------
void cpp_08_bin__method_getline ()
{
string line;

fs_bin_in.clear();
fs_bin_in.seekg (0, ios::beg);

fs_bin_out.clear();
fs_bin_out.seekp (0, ios::beg);

// ------ Body ------
while (getline (fs_bin_in, line)) fs_bin_out << line << '\n';
// ------------------

}


// --------------------------------------
void cpp_09_txt__method_ifstream_getline ()
{
char buffer[BUFFER_SIZE];

fs_txt_in.clear();
fs_txt_in.seekg (0, ios::beg);

fs_txt_out.clear();
fs_txt_out.seekp (0, ios::beg);

// ------ Body ------
while (fs_txt_in.getline (buffer, sizeof(buffer))) fs_txt_out << buffer << '\n';
// ------------------

}


// --------------------------------------
void cpp_09_bin__method_ifstream_getline ()
{
char buffer[BUFFER_SIZE];

fs_bin_in.clear();
fs_bin_in.seekg (0, ios::beg);

fs_bin_out.clear();
fs_bin_out.seekp (0, ios::beg);

// ------ Body ------
while (fs_bin_in.getline (buffer, sizeof(buffer))) fs_bin_out << buffer << '\n';
// ------------------

}

// --------------------------------------
void cpp_10_txt__iterators ()
{
char buffer[BUFFER_SIZE];

fs_txt_in.clear();
fs_txt_in.seekg (0, ios::beg);

fs_txt_out.clear();
fs_txt_out.seekp (0, ios::beg);

// ------ Body ------
fs_txt_in >> noskipws;
istream_iterator<char> in(fs_txt_in), eos;
ostream_iterator<char> out(fs_txt_out);
copy (in, eos, out);
// ------------------

}


// --------------------------------------
void cpp_10_bin__iterators ()
{
char buffer[BUFFER_SIZE];

fs_bin_in.clear();
fs_bin_in.seekg (0, ios::beg);

fs_bin_out.clear();
fs_bin_out.seekp (0, ios::beg);

// ------ Body ------
fs_bin_in >> noskipws;
istream_iterator<char> in(fs_bin_in), eos;
ostream_iterator<char> out(fs_bin_out);
copy (in, eos, out);
// ------------------

}

fp_txt_in = fopen(INPUT_TXT_FILE_NAME, "r");
assert(fp_txt_in);

fp_txt_out = fopen(OUTPUT_TXT_FILE_NAME, "w");
assert(fp_txt_out);

fp_bin_in = fopen(INPUT_BIN_FILE_NAME, "rb");
assert(fp_txt_in);

fp_bin_out = fopen(OUTPUT_BIN_FILE_NAME, "wb");
assert(fp_txt_out);


#ifdef UNIX_ENV
fd_txt_in = open(INPUT_TXT_FILE_NAME, O_RDONLY);
assert (fd_txt_in != -1);

fd_txt_out = open(OUTPUT_TXT_FILE_NAME, O_WRONLY);
assert (fd_txt_out != -1);

assert (fd_txt_in != fd_txt_out);


#endif

fs_txt_in.open (INPUT_TXT_FILE_NAME);
assert (fs_txt_in);
assert (fs_txt_in.is_open());

fs_txt_out.open (OUTPUT_TXT_FILE_NAME);
assert (fs_txt_out);
assert (fs_txt_out.is_open());


fs_bin_in.open (INPUT_BIN_FILE_NAME, ios::binary);
assert (fs_bin_in);
assert (fs_bin_in.is_open());

fs_bin_out.open (OUTPUT_BIN_FILE_NAME, ios::binary);
assert (fs_bin_out);
assert (fs_bin_out.is_open());

if (in_file_content.size() != file_size)
{
cerr << "Requested file size = " << file_size << endl;
cerr << "Input file size = " << in_file_content.size() << endl;

assert (in_file_content.size() == file_size);
}


// -------------------------------
// cout << "\t---> Test-" << ++test_no << " started" << endl;
cout << ".";
cout.flush();

// -------------------------------
MEASURE_WITH_NO_ARG (C_01_txt__functions_getc_putc);
assert (in_file_content == file_to_string(OUTPUT_TXT_FILE_NAME));

MEASURE_WITH_NO_ARG (C_01_bin__functions_getc_putc);
assert (in_file_content == file_to_string(OUTPUT_BIN_FILE_NAME));


MEASURE_WITH_NO_ARG (C_02_txt__functions_fgetc_fputc);
assert (in_file_content == file_to_string(OUTPUT_TXT_FILE_NAME));

MEASURE_WITH_NO_ARG (C_02_bin__functions_fgetc_fputc);
assert (in_file_content == file_to_string(OUTPUT_BIN_FILE_NAME));


MEASURE_WITH_NO_ARG (C_03_txt__functions_fread_fwrite);
assert (in_file_content == file_to_string(OUTPUT_TXT_FILE_NAME));

MEASURE_WITH_NO_ARG (C_03_bin__functions_fread_fwrite);
assert (in_file_content == file_to_string(OUTPUT_BIN_FILE_NAME));


#ifdef UNIX_ENV
MEASURE_WITH_NO_ARG (unix_C_04_txt__mmap);
assert (in_file_content == file_to_string(OUTPUT_TXT_FILE_NAME));
#endif


MEASURE_WITH_NO_ARG (cpp_01_txt__operators_in_out);
assert (in_file_content == file_to_string(OUTPUT_TXT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_01_bin__operators_in_out);
assert (in_file_content == file_to_string(OUTPUT_BIN_FILE_NAME));


MEASURE_WITH_NO_ARG (cpp_02_txt__methods_get_put);
assert (in_file_content == file_to_string(OUTPUT_TXT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_02_bin__methods_get_put);
assert (in_file_content == file_to_string(OUTPUT_BIN_FILE_NAME));


MEASURE_WITH_NO_ARG (cpp_03_txt__methods_sbumpc_sputc);
assert (in_file_content == file_to_string(OUTPUT_TXT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_03_bin__methods_sbumpc_sputc);
assert (in_file_content == file_to_string(OUTPUT_BIN_FILE_NAME));


MEASURE_WITH_NO_ARG (cpp_04_txt__method_sbumpc__op_out);
assert (in_file_content == file_to_string(OUTPUT_TXT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_04_bin__method_sbumpc__op_out);
assert (in_file_content == file_to_string(OUTPUT_BIN_FILE_NAME));


MEASURE_WITH_NO_ARG (cpp_05_txt__method_rdbuf__op_out);
assert (in_file_content == file_to_string(OUTPUT_TXT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_05_bin__method_rdbuf__op_out);
assert (in_file_content == file_to_string(OUTPUT_BIN_FILE_NAME));


MEASURE_WITH_NO_ARG (cpp_06_txt__methods_cpp_read_write__const_buf);
assert (in_file_content == file_to_string(OUTPUT_TXT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_06_bin__methods_cpp_read_write__const_buf);
assert (in_file_content == file_to_string(OUTPUT_BIN_FILE_NAME));


MEASURE_WITH_NO_ARG (cpp_07_txt__methods_cpp_read_write__max_buf);
assert (in_file_content == file_to_string(OUTPUT_TXT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_07_bin__methods_cpp_read_write__max_buf);
assert (in_file_content == file_to_string(OUTPUT_BIN_FILE_NAME));


MEASURE_WITH_NO_ARG (cpp_08_txt__method_getline);
assert ((in_file_content + in_file_tail_delta) == file_to_string(OUTPUT_TXT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_08_bin__method_getline);
assert ((in_file_content + in_file_tail_delta) == file_to_string(OUTPUT_BIN_FILE_NAME));


MEASURE_WITH_NO_ARG (cpp_09_txt__method_ifstream_getline);
assert ((in_file_content + in_file_tail_delta) == file_to_string(OUTPUT_TXT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_09_bin__method_ifstream_getline);
assert ((in_file_content + in_file_tail_delta) == file_to_string(OUTPUT_BIN_FILE_NAME));


MEASURE_WITH_NO_ARG (cpp_10_txt__iterators);
assert ((in_file_content + in_file_tail_delta) == file_to_string(OUTPUT_TXT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_10_bin__iterators);
assert ((in_file_content + in_file_tail_delta) == file_to_string(OUTPUT_BIN_FILE_NAME));


// -------------------------------
// cerr << "\t Test-" << test_no << " finished" << endl;


// -------------------------------
fs_txt_in.clear();
fs_txt_in.close();
assert (!fs_txt_in.is_open());

fs_txt_out.clear();
fs_txt_out.close();
assert (!fs_txt_out.is_open());


fs_bin_in.clear();
fs_bin_in.close();
assert (!fs_bin_in.is_open());

fs_bin_out.clear();
fs_bin_out.close();
assert (!fs_bin_out.is_open());


#ifdef UNIX_ENV
close (fd_txt_in);
close (fd_txt_out);
#endif


clearerr(fp_txt_in);
fclose(fp_txt_in);

clearerr(fp_txt_out);
fclose(fp_txt_out);


clearerr(fp_bin_in);
fclose(fp_bin_in);

clearerr(fp_bin_out);
fclose(fp_bin_out);


}

}

string exe_name (argv[0]);
cout << exe_name.substr (exe_name.find_last_of ("/\\") + 1) << " ";

for (long i = 1; i < argc; i++) cout << argv[i] << " ";
cout << endl;
cout << endl;

return 0;
}

--

Alex Vinokur

unread,
Apr 25, 2004, 5:17:00 AM4/25/04
to
// =====================================
// C/C++ Program Performance Measurement
// -------------------------------------
#define PROGRAM_NAME "Simple C/C++ Perfometer : Copying files"
#define PROGRAM_VERSION "Version CF-1.6"

// -------------------------------------
// Copyright (C) 2002-2004 Alex Vinokur
// mailto:ale...@connect.to
// http://up.to/alexv
// =====================================

// --------------------------------------


#if ((defined unix) || (defined __unix) || (defined unix__) || (defined __unix__))
#define UNIX_ENV
#endif

// ==============


#include <ctime>
#include <cassert>
#include <string>
#include <vector>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>

#include <iterator>
#include <algorithm>
#ifdef UNIX_ENV
#include <fcntl.h>
#include <sys/mman.h>
#endif

using namespace std;


// --------------------------------------


typedef unsigned long ulong;
typedef unsigned int uint;


// --------------------------------------


#define MAX_VALUE(x,y) ((x) > (y) ? (x) : (y))

// --------------------------------------
string get_compiler_info()
{
ostringstream oss;

ostringstream tss;
string str;


// ------ GNU gcc ------
#ifdef __GNUC__

oss.str("");
tss.str("");
str.erase();

oss << "GNU gcc " << __GNUC__;


#ifdef __GNUC_MINOR__
oss << "." << __GNUC_MINOR__;
#ifdef __GNUC_PATCHLEVEL__
#if (__GNUC_PATCHLEVEL__)
oss << "." << __GNUC_PATCHLEVEL__;
#endif
#endif
#endif

#if (__CYGWIN32__ || __CYGWIN__)
oss << " (CYGWIN)";
#endif

#if (__MINGW32__ || __MINGW__ )
oss << " (MINGW)";
#endif

#if (__DJGPP__)
oss << " (DGGPP " << __DJGPP__;
#ifdef __DJGPP_MINOR__
oss << "." << __DJGPP_MINOR__;
#endif
oss << ")";
#endif

#endif

// ------ Microsoft C++ ------
#ifdef _MSC_VER

oss.str("");
tss.str("");
str.erase();

oss << "Microsoft C++ ";


tss << _MSC_VER;
str = tss.str();
assert (str.size() == 4);
oss << str[0] << str[1] << "." << str[2] << str[3];

#ifdef _MANAGED

#if (_MANAGED)
oss << " (Managed)";
#else
assert (0);
oss << " (Unmanaged)";
#endif
#else

oss << " (Unmanaged)";
#endif

#endif

// ------ Intel C++ ------


#ifdef __INTEL_COMPILER
oss.str("");
tss.str("");
str.erase();

oss << "Intel C++ ";
tss << __INTEL_COMPILER;

str = tss.str();
assert (str.size() == 3);

oss << str[0] << "." << str[1];
#endif


// ------ Borland C++ ------
#ifdef __BCPLUSPLUS__

oss.str("");
tss.str("");
str.erase();

oss << "Borland C++ ";


tss << hex << __BCPLUSPLUS__;
str = tss.str();
assert (str.size() == 3);
oss << str[0] << "." << str[1] << "." << str[2];
#endif


// ------ Digital Mars C++ ------
#ifdef __DMC__

oss.str("");
tss.str("");
str.erase();

#ifndef __DMC_VERSION_STRING__


#error __DMC_VERSION_STRING__ Not Defined
#endif
oss << __DMC_VERSION_STRING__;
#endif

return oss.str();

} // get_compiler_info


// --------------------------------------
void show_compiler_info()
{
const string str(get_compiler_info());

if (str.empty()) return;

cout << string (str.size(), '-') << endl;
cout << str << endl;
cout << string (str.size(), '-') << endl;
}

// --------------------------------------


static vector<string> foo_names;
static vector<string>::iterator iter_names;
static vector<vector<clock_t> > used_time;

static ulong test_no = 0;
static ulong file_size;
static string in_file_content;

static string in_file_tail_delta;

static uint foo_setw = 0;

// ------------------------------


#define INPUT_TXT_FILE_NAME "z-txt.in"
#define OUTPUT_TXT_FILE_NAME "z-txt.out"
#define INPUT_BIN_FILE_NAME "z-bin.in"
#define OUTPUT_BIN_FILE_NAME "z-bin.out"


#define ROWS_IN_INPUT_FILE 7

static FILE* fp_txt_in;
static FILE* fp_txt_out;
static FILE* fp_bin_in;
static FILE* fp_bin_out;

#ifdef UNIX_ENV
static int fd_txt_in;
static int fd_txt_out;
static int fd_bin_in;
static int fd_bin_out;
#endif

static ifstream fs_txt_in;
static ofstream fs_txt_out;
static ifstream fs_bin_in;
static ofstream fs_bin_out;


#define BUFFER_SIZE 4096

static char* mbuffer;

fin_i.clear();

char ch;

// ---------------------
return ret_str;

} // file_to_string (1)

fin.close();
assert (!fin.is_open());

return ret_str;

} // file_to_string (2)


// ------------------------------


void fill_input_file (uint total_full_rows = ROWS_IN_INPUT_FILE)
{
assert (total_full_rows > 0);
if (file_size < total_full_rows)
{
total_full_rows = file_size;
}
assert (total_full_rows <= file_size);

ofstream test_txt_infile (INPUT_TXT_FILE_NAME);
assert (test_txt_infile);
assert (test_txt_infile.is_open());

ofstream test_bin_infile (INPUT_BIN_FILE_NAME);
assert (test_bin_infile);
assert (test_bin_infile.is_open());


const uint row_size = file_size/total_full_rows;

char ch = 0;

ulong counter = 0;
for (ulong row_no = 0; row_no < total_full_rows; row_no++)
{
for (ulong ch_no = 0; ch_no < row_size; ch_no++)
{
while (!isprint(ch)) ch++;
assert (counter < file_size);

test_txt_infile << ch;
test_bin_infile << ch;
ch++;
counter++;
if (counter == file_size) break;

if (ch == SCHAR_MAX) ch = 0;
}

if (counter == file_size) break;
assert (counter < file_size);

test_txt_infile << '\n';
test_bin_infile << '\n';
counter++;
if (counter == file_size) break;
}

/*


for (ulong i = 0; i < file_size; i++)
{
while (!isprint(ch)) ch++;

test_txt_infile << ch++;


if (ch == SCHAR_MAX) ch = 0;
}

*/

test_txt_infile.close();
assert (!test_txt_infile.is_open());

test_bin_infile.close();
assert (!test_bin_infile.is_open());

const string str_txt (file_to_string (INPUT_TXT_FILE_NAME));
const string str_bin (file_to_string (INPUT_BIN_FILE_NAME));
assert (str_txt == str_bin);

const string tail_txt (((*str_txt.rbegin()) == '\n') ? "" : "\n");
const string tail_bin (((*str_bin.rbegin()) == '\n') ? "" : "\n");
assert (tail_txt == tail_bin);

in_file_content = str_txt;
in_file_tail_delta = tail_txt;

}


// --------------------------------------
void C_01_txt__functions_getc_putc ()
{
int int_ch;

clearerr(fp_txt_in);
rewind (fp_txt_in);

clearerr(fp_txt_out);
rewind (fp_txt_out);

// ------ Body ------


while ((int_ch = getc(fp_txt_in)) != EOF) putc(int_ch, fp_txt_out);

// ------------------
}

// --------------------------------------


void C_01_bin__functions_getc_putc ()
{
int int_ch;

clearerr(fp_bin_in);
rewind (fp_bin_in);

clearerr(fp_bin_out);
rewind (fp_bin_out);

// ------ Body ------


while ((int_ch = getc(fp_bin_in)) != EOF) putc(int_ch, fp_bin_out);

// ------------------
}


// --------------------------------------


void C_02_txt__functions_fgetc_fputc ()
{
int int_ch;

clearerr(fp_txt_in);
rewind (fp_txt_in);

clearerr(fp_txt_out);
rewind (fp_txt_out);

// ------ Body ------


while ((int_ch = fgetc(fp_txt_in)) != EOF) fputc(int_ch, fp_txt_out);

// ------------------
}


// --------------------------------------


void C_02_bin__functions_fgetc_fputc ()
{
int int_ch;

clearerr(fp_bin_in);
rewind (fp_bin_in);

clearerr(fp_bin_out);
rewind (fp_bin_out);

// ------ Body ------


while ((int_ch = fgetc(fp_bin_in)) != EOF) fputc(int_ch, fp_bin_out);

// ------------------
}


// --------------------------------------
void C_03_txt__functions_fread_fwrite ()


{
char cbuffer[BUFFER_SIZE];
size_t nread;

clearerr(fp_txt_in);
rewind (fp_txt_in);

clearerr(fp_txt_out);
rewind (fp_txt_out);

// ------ Body ------


while ((nread = fread(cbuffer, sizeof(char), sizeof(cbuffer), fp_txt_in)) > 0)
{
fwrite(cbuffer, sizeof(char), nread, fp_txt_out);
}

// ------------------
}


// --------------------------------------
void C_03_bin__functions_fread_fwrite ()


{
char cbuffer[BUFFER_SIZE];
size_t nread;

clearerr(fp_bin_in);
rewind (fp_bin_in);

clearerr(fp_bin_out);
rewind (fp_bin_out);

// ------ Body ------


while ((nread = fread(cbuffer, sizeof(char), sizeof(cbuffer), fp_bin_in)) > 0)
{
fwrite(cbuffer, sizeof(char), nread, fp_bin_out);
}
// ------------------
}

#ifdef UNIX_ENV
// --------------------------------------
void unix_C_04_txt__mmap ()
{
off_t rc;
rc = lseek(fd_txt_in, 0, SEEK_SET);
rc = lseek(fd_txt_out, 0, SEEK_SET);

// ------ Body ------
char* ptr = (char*)mmap(0, file_size, PROT_READ, MAP_SHARED, fd_txt_in, 0);
assert (ptr != MAP_FAILED);
write(fd_txt_out, ptr, file_size);
munmap(ptr, file_size);

// ------------------
}

// --------------------------------------
void unix_C_04_bin__mmap ()
{
off_t rc;
rc = lseek(fd_bin_in, 0, SEEK_SET);
rc = lseek(fd_bin_out, 0, SEEK_SET);

// ------ Body ------
char* ptr = (char*)mmap(0, file_size, PROT_READ, MAP_SHARED, fd_bin_in, 0);
assert (ptr != MAP_FAILED);
write(fd_bin_out, ptr, file_size);
munmap(ptr, file_size);
// ------------------
}

#endif


// --------------------------------------
void cpp_01_txt__operators_in_out ()
{
char ch;

fs_txt_in.clear();
fs_txt_in.seekg (0, ios::beg);

fs_txt_out.clear();
fs_txt_out.seekp (0, ios::beg);

// ------ Body ------
fs_txt_in.unsetf(ios::skipws);
while (fs_txt_in >> ch) fs_txt_out << ch;
// ------------------
}


// --------------------------------------


void cpp_01_bin__operators_in_out ()
{
char ch;

fs_bin_in.clear();
fs_bin_in.seekg (0, ios::beg);

fs_bin_out.clear();
fs_bin_out.seekp (0, ios::beg);

// ------ Body ------
fs_bin_in.unsetf(ios::skipws);
while (fs_bin_in >> ch) fs_bin_out << ch;
// ------------------
}


// --------------------------------------


void cpp_02_txt__methods_get_put ()
{
char ch;

fs_txt_in.clear();
fs_txt_in.seekg (0, ios::beg);

fs_txt_out.clear();
fs_txt_out.seekp (0, ios::beg);

// ------ Body ------
while (fs_txt_in.get(ch)) fs_txt_out.put(ch);
// ------------------
}


// --------------------------------------


void cpp_02_bin__methods_get_put ()
{
char ch;

fs_bin_in.clear();
fs_bin_in.seekg (0, ios::beg);

fs_bin_out.clear();
fs_bin_out.seekp (0, ios::beg);

// ------ Body ------
while (fs_bin_in.get(ch)) fs_bin_out.put(ch);
// ------------------
}


// --------------------------------------


void cpp_03_txt__methods_sbumpc_sputc ()
{
char ch;

fs_txt_in.clear();
fs_txt_in.seekg (0, ios::beg);

fs_txt_out.clear();
fs_txt_out.seekp (0, ios::beg);

// ------ Body ------
while ((ch = fs_txt_in.rdbuf()->sbumpc()) != EOF) fs_txt_out.rdbuf()->sputc(ch);
// ------------------
}


// --------------------------------------


void cpp_03_bin__methods_sbumpc_sputc ()
{
char ch;

fs_bin_in.clear();
fs_bin_in.seekg (0, ios::beg);

fs_bin_out.clear();
fs_bin_out.seekp (0, ios::beg);

// ------ Body ------
while ((ch = fs_bin_in.rdbuf()->sbumpc()) != EOF) fs_bin_out.rdbuf()->sputc(ch);
// ------------------
}


// --------------------------------------


void cpp_04_txt__method_sbumpc__op_out ()
{
char ch;

fs_txt_in.clear();
fs_txt_in.seekg (0, ios::beg);

fs_txt_out.clear();
fs_txt_out.seekp (0, ios::beg);

// ------ Body ------
ch = fs_txt_in.rdbuf()->sbumpc();
fs_txt_out << ch;

while (ch != EOF)
{


fs_txt_out << fs_txt_in.rdbuf();
ch = fs_txt_in.rdbuf()->sbumpc();
}

}

// --------------------------------------
void cpp_04_bin__method_sbumpc__op_out ()
{
char ch;

fs_bin_in.clear();
fs_bin_in.seekg (0, ios::beg);

fs_bin_out.clear();
fs_bin_out.seekp (0, ios::beg);

// ------ Body ------
ch = fs_bin_in.rdbuf()->sbumpc();
fs_bin_out << ch;

while (ch != EOF)
{


fs_bin_out << fs_bin_in.rdbuf();
ch = fs_bin_in.rdbuf()->sbumpc();
}

}


// --------------------------------------
void cpp_05_txt__method_rdbuf__op_out ()
{
fs_txt_in.clear();
fs_txt_in.seekg (0, ios::beg);

fs_txt_out.clear();
fs_txt_out.seekp (0, ios::beg);

// ------ Body ------
fs_txt_out << fs_txt_in.rdbuf();
// ------------------

}


// --------------------------------------


void cpp_05_bin__method_rdbuf__op_out ()
{
fs_bin_in.clear();
fs_bin_in.seekg (0, ios::beg);

fs_bin_out.clear();
fs_bin_out.seekp (0, ios::beg);

// ------ Body ------
fs_bin_out << fs_bin_in.rdbuf();
// ------------------

}


// --------------------------------------


void cpp_06_txt__methods_cpp_read_write__const_buf ()
{
char cbuffer[BUFFER_SIZE];

fs_txt_in.clear();
fs_txt_in.seekg (0, ios::beg);

fs_txt_out.clear();
fs_txt_out.seekp (0, ios::beg);

// ------ Body ------


while (!fs_txt_in.eof())
{
fs_txt_in.read (cbuffer, sizeof(cbuffer));
fs_txt_out.write (cbuffer, fs_txt_in.gcount());
}

// ------------------
}


// --------------------------------------


void cpp_06_bin__methods_cpp_read_write__const_buf ()
{
char cbuffer[BUFFER_SIZE];

fs_bin_in.clear();
fs_bin_in.seekg (0, ios::beg);

fs_bin_out.clear();
fs_bin_out.seekp (0, ios::beg);

// ------ Body ------


while (!fs_bin_in.eof())
{
fs_bin_in.read (cbuffer, sizeof(cbuffer));
fs_bin_out.write (cbuffer, fs_bin_in.gcount());
}

// ------------------
}


// --------------------------------------


void cpp_07_txt__methods_cpp_read_write__max_buf ()
{
fs_txt_in.clear();
fs_txt_in.seekg (0, ios::beg);

fs_txt_out.clear();
fs_txt_out.seekp (0, ios::beg);

// ------ Body ------
fs_txt_in.read (mbuffer, file_size);
fs_txt_out.write (mbuffer, file_size);
// ------------------

}

// --------------------------------------


void cpp_07_bin__methods_cpp_read_write__max_buf ()
{
fs_bin_in.clear();
fs_bin_in.seekg (0, ios::beg);

fs_bin_out.clear();
fs_bin_out.seekp (0, ios::beg);

// ------ Body ------
fs_bin_in.read (mbuffer, file_size);
fs_bin_out.write (mbuffer, file_size);
// ------------------

}

// --------------------------------------


void cpp_08_txt__method_getline ()
{
string line;

fs_txt_in.clear();
fs_txt_in.seekg (0, ios::beg);

fs_txt_out.clear();
fs_txt_out.seekp (0, ios::beg);

// ------ Body ------


while (getline (fs_txt_in, line)) fs_txt_out << line << '\n';

// ------------------

}

// --------------------------------------


void cpp_08_bin__method_getline ()
{
string line;

fs_bin_in.clear();
fs_bin_in.seekg (0, ios::beg);

fs_bin_out.clear();
fs_bin_out.seekp (0, ios::beg);

// ------ Body ------


while (getline (fs_bin_in, line)) fs_bin_out << line << '\n';

// ------------------

}


// --------------------------------------


void cpp_09_txt__method_ifstream_getline ()
{
char buffer[BUFFER_SIZE];

fs_txt_in.clear();
fs_txt_in.seekg (0, ios::beg);

fs_txt_out.clear();
fs_txt_out.seekp (0, ios::beg);

// ------ Body ------


while (fs_txt_in.getline (buffer, sizeof(buffer))) fs_txt_out << buffer << '\n';

// ------------------

}


// --------------------------------------


void cpp_09_bin__method_ifstream_getline ()
{
char buffer[BUFFER_SIZE];

fs_bin_in.clear();
fs_bin_in.seekg (0, ios::beg);

fs_bin_out.clear();
fs_bin_out.seekp (0, ios::beg);

// ------ Body ------


while (fs_bin_in.getline (buffer, sizeof(buffer))) fs_bin_out << buffer << '\n';

// ------------------

}


// --------------------------------------
void cpp_10_txt__iostream_iterators__copy ()


{
fs_txt_in.clear();
fs_txt_in.seekg (0, ios::beg);

fs_txt_out.clear();
fs_txt_out.seekp (0, ios::beg);

// ------ Body ------


fs_txt_in >> noskipws;
istream_iterator<char> in(fs_txt_in), eos;
ostream_iterator<char> out(fs_txt_out);
copy (in, eos, out);

// ------------------

}


// --------------------------------------
void cpp_10_bin__iostream_iterators__copy ()


{
fs_bin_in.clear();
fs_bin_in.seekg (0, ios::beg);

fs_bin_out.clear();
fs_bin_out.seekp (0, ios::beg);

// ------ Body ------


fs_bin_in >> noskipws;
istream_iterator<char> in(fs_bin_in), eos;
ostream_iterator<char> out(fs_bin_out);
copy (in, eos, out);

// ------------------

}

// --------------------------------------
void cpp_11_txt__iostreambuf_iterators__copy ()


{
fs_txt_in.clear();
fs_txt_in.seekg (0, ios::beg);

fs_txt_out.clear();
fs_txt_out.seekp (0, ios::beg);

// ------ Body ------
fs_txt_in >> noskipws;
istreambuf_iterator<char> in(fs_txt_in), eos;
ostreambuf_iterator<char> out(fs_txt_out);
copy (in, eos, out);
// ------------------

}


// --------------------------------------
void cpp_11_bin__iostreambuf_iterators__copy ()


{
fs_bin_in.clear();
fs_bin_in.seekg (0, ios::beg);

fs_bin_out.clear();
fs_bin_out.seekp (0, ios::beg);

// ------ Body ------
fs_bin_in >> noskipws;
istreambuf_iterator<char> in(fs_bin_in), eos;
ostreambuf_iterator<char> out(fs_bin_out);


copy (in, eos, out);
// ------------------

}

// --------------------------------------
struct char_identity
{
char operator()(char ch) const { return ch; }
};


// --------------------------------------
void cpp_12_txt__iostreambuf_iterators__transform ()


{
fs_txt_in.clear();
fs_txt_in.seekg (0, ios::beg);

fs_txt_out.clear();
fs_txt_out.seekp (0, ios::beg);

// ------ Body ------
fs_txt_in >> noskipws;
istreambuf_iterator<char> in(fs_txt_in), eos;
ostreambuf_iterator<char> out(fs_txt_out);
transform(in, eos, out, char_identity());
// ------------------

}


// --------------------------------------
void cpp_12_bin__iostreambuf_iterators__transform ()


{
fs_bin_in.clear();
fs_bin_in.seekg (0, ios::beg);

fs_bin_out.clear();
fs_bin_out.seekp (0, ios::beg);

// ------ Body ------
fs_bin_in >> noskipws;
istreambuf_iterator<char> in(fs_bin_in), eos;
ostreambuf_iterator<char> out(fs_bin_out);
transform(in, eos, out, char_identity());
// ------------------

}

fp_txt_in = fopen(INPUT_TXT_FILE_NAME, "r");
assert(fp_txt_in);

fp_txt_out = fopen(OUTPUT_TXT_FILE_NAME, "w");
assert(fp_txt_out);

fp_bin_in = fopen(INPUT_BIN_FILE_NAME, "rb");
assert(fp_txt_in);

fp_bin_out = fopen(OUTPUT_BIN_FILE_NAME, "wb");
assert(fp_txt_out);


#ifdef UNIX_ENV
fd_txt_in = open(INPUT_TXT_FILE_NAME, O_RDONLY);
assert (fd_txt_in != -1);

fd_txt_out = open(OUTPUT_TXT_FILE_NAME, O_WRONLY);
assert (fd_txt_out != -1);

assert (fd_txt_in != fd_txt_out);


fd_bin_in = open(INPUT_BIN_FILE_NAME, O_RDONLY | O_BINARY);
assert (fd_bin_in != -1);

fd_bin_out = open(OUTPUT_BIN_FILE_NAME, O_WRONLY | O_BINARY);
assert (fd_bin_out != -1);

assert (fd_bin_in != fd_bin_out);

#endif

fs_txt_in.open (INPUT_TXT_FILE_NAME);
assert (fs_txt_in);
assert (fs_txt_in.is_open());

fs_txt_out.open (OUTPUT_TXT_FILE_NAME);
assert (fs_txt_out);
assert (fs_txt_out.is_open());


fs_bin_in.open (INPUT_BIN_FILE_NAME, ios::binary);
assert (fs_bin_in);
assert (fs_bin_in.is_open());

fs_bin_out.open (OUTPUT_BIN_FILE_NAME, ios::binary);
assert (fs_bin_out);
assert (fs_bin_out.is_open());

if (in_file_content.size() != file_size)
{
cerr << "Requested file size = " << file_size << endl;
cerr << "Input file size = " << in_file_content.size() << endl;

assert (in_file_content.size() == file_size);
}


// -------------------------------
// cout << "\t---> Test-" << ++test_no << " started" << endl;
cout << ".";
cout.flush();

// -------------------------------


MEASURE_WITH_NO_ARG (C_01_txt__functions_getc_putc);
assert (in_file_content == file_to_string(OUTPUT_TXT_FILE_NAME));

MEASURE_WITH_NO_ARG (C_01_bin__functions_getc_putc);
assert (in_file_content == file_to_string(OUTPUT_BIN_FILE_NAME));


MEASURE_WITH_NO_ARG (C_02_txt__functions_fgetc_fputc);
assert (in_file_content == file_to_string(OUTPUT_TXT_FILE_NAME));

MEASURE_WITH_NO_ARG (C_02_bin__functions_fgetc_fputc);
assert (in_file_content == file_to_string(OUTPUT_BIN_FILE_NAME));


MEASURE_WITH_NO_ARG (C_03_txt__functions_fread_fwrite);
assert (in_file_content == file_to_string(OUTPUT_TXT_FILE_NAME));

MEASURE_WITH_NO_ARG (C_03_bin__functions_fread_fwrite);
assert (in_file_content == file_to_string(OUTPUT_BIN_FILE_NAME));


#ifdef UNIX_ENV
MEASURE_WITH_NO_ARG (unix_C_04_txt__mmap);
assert (in_file_content == file_to_string(OUTPUT_TXT_FILE_NAME));

MEASURE_WITH_NO_ARG (unix_C_04_bin__mmap);
assert (in_file_content == file_to_string(OUTPUT_BIN_FILE_NAME));
#endif


MEASURE_WITH_NO_ARG (cpp_10_txt__iostream_iterators__copy);


assert ((in_file_content + in_file_tail_delta) == file_to_string(OUTPUT_TXT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_10_bin__iostream_iterators__copy);


assert ((in_file_content + in_file_tail_delta) == file_to_string(OUTPUT_BIN_FILE_NAME));


MEASURE_WITH_NO_ARG (cpp_11_txt__iostreambuf_iterators__copy);


assert ((in_file_content + in_file_tail_delta) == file_to_string(OUTPUT_TXT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_11_bin__iostreambuf_iterators__copy);


assert ((in_file_content + in_file_tail_delta) == file_to_string(OUTPUT_BIN_FILE_NAME));


MEASURE_WITH_NO_ARG (cpp_12_txt__iostreambuf_iterators__transform);


assert ((in_file_content + in_file_tail_delta) == file_to_string(OUTPUT_TXT_FILE_NAME));

MEASURE_WITH_NO_ARG (cpp_12_bin__iostreambuf_iterators__transform);


assert ((in_file_content + in_file_tail_delta) == file_to_string(OUTPUT_BIN_FILE_NAME));

// -------------------------------
// cerr << "\t Test-" << test_no << " finished" << endl;


// -------------------------------
fs_txt_in.clear();
fs_txt_in.close();
assert (!fs_txt_in.is_open());

fs_txt_out.clear();
fs_txt_out.close();
assert (!fs_txt_out.is_open());


fs_bin_in.clear();
fs_bin_in.close();
assert (!fs_bin_in.is_open());

fs_bin_out.clear();
fs_bin_out.close();
assert (!fs_bin_out.is_open());


#ifdef UNIX_ENV
close (fd_txt_in);
close (fd_txt_out);

close (fd_bin_in);
close (fd_bin_out);
#endif


clearerr(fp_txt_in);
fclose(fp_txt_in);

clearerr(fp_txt_out);
fclose(fp_txt_out);


clearerr(fp_bin_in);
fclose(fp_bin_in);

clearerr(fp_bin_out);
fclose(fp_bin_out);


}


}

string exe_name (argv[0]);
cout << exe_name.substr (exe_name.find_last_of ("/\\") + 1) << " ";

for (long i = 1; i < argc; i++) cout << argv[i] << " ";
cout << endl;
cout << endl;

return 0;
}


0 new messages