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

[C++] Simple Perfometer (Sample)

0 views
Skip to first unread message

Alex Vinokur

unread,
Sep 28, 2003, 5:26:54 AM9/28/03
to
================================
[C++] Simple Perfometer (Sample)
================================

#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

static int value = rand();
static int sink = 0;

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

int test_no = 0;

// ------ with dummy ------
void foo__dummy_start () { sink += value; }
void foo__dummy_end () { sink += value; }


// ------ with body ------
void foo__body__no_args () { sink += value; }

void foo__body__char_arg_unused (char) { sink += value; }
void foo__body__char_arg_used (char arg) { sink += arg; }
void foo__body__char_ref_unused (const char&) { sink += value; }
void foo__body__char_ref_used (const char& arg) { sink += arg; }


void foo__body__short_arg_unused (short) { sink += value; }
void foo__body__short_arg_used (short arg) { sink += arg; }
void foo__body__short_ref_unused (const short&) { sink += value; }
void foo__body__short_ref_used (const short& arg) { sink += arg; }


void foo__body__int_arg_unused (int) { sink += value; }
void foo__body__int_arg_used (int arg) { sink += arg; }
void foo__body__int_ref_unused (const int&) { sink += value; }
void foo__body__int_ref_used (const int& arg) { sink += arg; }


// ------ with no body ------
void foo__no_body__no_args () {}

void foo__no_body__char_arg_unused (char) {}
void foo__no_body__char_ref_unused (const char&) {}


void foo__no_body__short_arg_unused (short) {}
void foo__no_body__short_ref_unused (const short&) {}


void foo__no_body__int_arg_unused (int) {}
void foo__no_body__int_ref_unused (const int&) {}


#define MEASURE_IT(x, y) \
start_time = clock(); \
assert (start_time != -1); \
for (int k = 0; k < no_of_repetitions; k++) x;\
end_time = clock(); \
assert (end_time != -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 (int no_of_repetitions)
{
const char ch_val = 'a';
const short sh_val = 'a';
const int int_val = 'a';


clock_t start_time;
clock_t end_time;
vector<clock_t> elapsed_time_vect;

cerr << "\t---> Test-" << ++test_no << " started" << endl;
// -------------------------------
MEASURE_WITH_NO_ARG (foo__dummy_start);

// -------------------------------
MEASURE_WITH_NO_ARG (foo__body__no_args);

MEASURE_WITH_ARG (foo__body__char_arg_unused, ch_val);
MEASURE_WITH_ARG (foo__body__char_arg_used, ch_val);
MEASURE_WITH_ARG (foo__body__char_ref_unused, ch_val);
MEASURE_WITH_ARG (foo__body__char_ref_used, ch_val);

MEASURE_WITH_ARG (foo__body__short_arg_unused, sh_val);
MEASURE_WITH_ARG (foo__body__short_arg_used, sh_val);
MEASURE_WITH_ARG (foo__body__short_ref_unused, sh_val);
MEASURE_WITH_ARG (foo__body__short_ref_used, sh_val);

MEASURE_WITH_ARG (foo__body__int_arg_unused, int_val);
MEASURE_WITH_ARG (foo__body__int_arg_used, int_val);
MEASURE_WITH_ARG (foo__body__int_ref_unused, int_val);
MEASURE_WITH_ARG (foo__body__int_ref_used, int_val);

// -------------------------------
MEASURE_WITH_NO_ARG (foo__no_body__no_args);

MEASURE_WITH_ARG (foo__no_body__char_arg_unused, ch_val);
MEASURE_WITH_ARG (foo__no_body__char_ref_unused, ch_val);

MEASURE_WITH_ARG (foo__no_body__short_arg_unused, sh_val);
MEASURE_WITH_ARG (foo__no_body__short_ref_unused, sh_val);

MEASURE_WITH_ARG (foo__no_body__int_arg_unused, int_val);
MEASURE_WITH_ARG (foo__no_body__int_ref_unused, int_val);


// -------------------------------
MEASURE_WITH_NO_ARG (foo__dummy_end);
}


// ------------
void show (int no_of_tests)
{
#define FACTOR 100
clock_t ticks;
clock_t sum;

iter_names = find (foo_names.begin(), foo_names.end(), "foo__body__int_arg_used");
assert (iter_names != foo_names.end());

const int normalizator_no = distance (foo_names.begin(), iter_names);

assert (no_of_tests == used_time[normalizator_no].size());

sum = 0;
for (int k = 0; k < used_time[normalizator_no].size(); k++)
{
sum += used_time[normalizator_no][k];
}
const clock_t normalizator_value = sum/used_time[normalizator_no].size();


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

for (int i = 0; i < foo_names.size(); i++)
{
sum = 0;
assert (no_of_tests == used_time[i].size());
for (int k = 0; k < used_time[i].size(); k++) sum += used_time[i][k];
ticks = sum/used_time[i].size();

cout << setw(30)
<< std::left
<< foo_names[i]
<< " : "
<< setw(6)
<< std::right
<< ticks
<< " ticks"
<< " ("
<< (float(ticks)/float(CLOCKS_PER_SEC))
<< " secs; "
<< setw(4)
<< std::right
<< ((ticks * FACTOR)/normalizator_value)
<< " normalized units)"
<< endl;
}

}


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

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

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

const int no_of_tests = atoi (argv[1]);
assert (no_of_tests > 0);

const int no_of_repetitions = atoi (argv[2]);
assert (no_of_repetitions > 0);


cerr << "\t### Number of tests : " << no_of_tests << endl;
cerr << "\t### Number of repetitions : " << no_of_repetitions << endl;
cerr << "\t### CLOCKS_PER_SEC : " << CLOCKS_PER_SEC << endl;
cerr << endl;


// -----------------------------
for (int i = 0; i < no_of_tests; i++) measure (no_of_repetitions);
show (no_of_tests);

return (sink%2);
}

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

Alex Vinokur

unread,
Sep 30, 2003, 12:26:42 PM9/30/03
to
// =====================================
// C/C++ Program Performance Measurement
// -------------------------------------
// Simple Perfometer
// Version 1.1
// =====================================

int test_no = 0;

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 (int no_of_repetitions)
{
const char ch_val = 'a';
const short sh_val = 'a';
const int int_val = 'a';


clock_t start_time;
clock_t end_time;
vector<clock_t> elapsed_time_vect;

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

// -------------------------------
MEASURE_WITH_NO_ARG (foo__body__no_args);

// -------------------------------
MEASURE_WITH_NO_ARG (foo__no_body__no_args);


// -------------------------------
MEASURE_WITH_NO_ARG (foo__dummy_end);

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

<< " (";
cout.setf(ios::fixed, ios::floatfield);
cout << setprecision (3)


<< (float(ticks)/float(CLOCKS_PER_SEC))
<< " secs; "
<< setw(4)
<< std::right
<< ((ticks * FACTOR)/normalizator_value)
<< " normalized units)"
<< endl;
}

}

// ------------
void run (int no_of_runs, int no_of_tests, int no_of_repetitions)
{
for (int 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"<< endl;
for (int k = 0; k < no_of_tests; k++) measure (no_of_repetitions);
show (no_of_tests);
cerr << " Run-" << (i + 1) << " of " << no_of_runs << " : Finished"<< endl << endl;
}
}


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

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

if (!(argc >= 3))
{
cout << "\tUSAGE : "
<< argv[0]
<< " "
<< "<No. of tests> <No. of repetitions> [<No. of runs>]"


<< endl;
return 1;
}
assert (argc >= 3);


const int no_of_tests = atoi (argv[1]);
assert (no_of_tests > 0);

const int no_of_repetitions = atoi (argv[2]);
assert (no_of_repetitions > 0);

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

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 (sink%2);
}

--


==========================
Alex Vinokur
mailto:ale...@connect.to

http://lists.sourceforge.net/lists/listinfo/cpp-perfometer-users
news://news.gmane.org/gmane.comp.lang.c++.perfometer
==========================


0 new messages