#include <stddef.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
template <class T>
inline int operator!=(const T& x, const T& y) {
return !(x == y);
}
struct Double {
double value;
Double() {}
Double(const double& x) : value(x) {}
operator double() { return value; }
};
inline Double operator+(const Double& x, const Double& y) {
return Double(x.value + y.value);
}
struct double_pointer {
double* current;
double_pointer() {}
double_pointer(double* x) : current(x) {}
double& operator*() const { return *current; }
double_pointer& operator++() {
++current;
return *this;
}
double_pointer operator++(int) {
double_pointer tmp = *this;
++*this;
return tmp;
}
double_pointer& operator--() {
--current;
return *this;
}
double_pointer operator--(int) {
double_pointer tmp = *this;
--*this;
return tmp;
}
};
inline int operator==(const double_pointer& x,
const double_pointer& y) {
return x.current == y.current;
}
struct Double_pointer {
Double* current;
Double_pointer() {}
Double_pointer(Double* x) : current(x) {}
Double& operator*() const { return *current; }
Double_pointer& operator++() {
++current;
return *this;
}
Double_pointer operator++(int) {
Double_pointer tmp = *this;
++*this;
return tmp;
}
Double_pointer& operator--() {
--current;
return *this;
}
Double_pointer operator--(int) {
Double_pointer tmp = *this;
--*this;
return tmp;
}
};
inline int operator==(const Double_pointer& x,
const Double_pointer& y) {
return x.current == y.current;
}
template <class RandomAccessIterator, class T>
struct reverse_iterator {
RandomAccessIterator current;
reverse_iterator(RandomAccessIterator x) : current(x) {}
T& operator*() const {
RandomAccessIterator tmp = current;
return *(--tmp);
}
reverse_iterator<RandomAccessIterator, T>& operator++() {
--current;
return *this;
}
reverse_iterator<RandomAccessIterator, T> operator++(int) {
reverse_iterator<RandomAccessIterator, T> tmp = *this;
++*this;
return tmp;
}
reverse_iterator<RandomAccessIterator, T>& operator--() {
++current;
return *this;
}
reverse_iterator<RandomAccessIterator, T> operator--(int) {
reverse_iterator<RandomAccessIterator, T> tmp = *this;
--*this;
return tmp;
}
};
template <class RandomAccessIterator, class T>
inline int operator==(const reverse_iterator<RandomAccessIterator, T>& x,
const reverse_iterator<RandomAccessIterator, T>& y) {
return x.current == y.current;
}
struct {
double operator()(const double& x, const double& y) {return x + y; }
Double operator()(const Double& x, const Double& y) {return x + y; }
} plus;
template <class Iterator, class Number>
Number accumulate(Iterator first, Iterator last, Number result) {
while (first != last) result = plus(result, *first++);
return result;
}
int iterations = 25000;
#define SIZE 2000
int current_test = 0;
double result_times[20];
void summarize() {
printf("\ntest absolute additions ratio with\n");
printf("number time per second test0\n\n");
int i;
double millions = (double(SIZE) * iterations)/1000000.;
for (i = 0; i < current_test; ++i)
printf("%2i %5.2fsec %5.2fM %.2f\n",
i,
result_times[i],
millions/result_times[i],
result_times[i]/result_times[0]);
double gmean_times = 0.;
double total_absolute_times = 0.; // sam added 12/05/95
double gmean_rate = 0.;
double gmean_ratio = 0.;
for (i = 0; i < current_test; ++i) {
total_absolute_times += result_times[i]; // sam added 12/05/95
gmean_times += log(result_times[i]);
gmean_rate += log(millions/result_times[i]);
gmean_ratio += log(result_times[i]/result_times[0]);
}
printf("mean: %5.2fsec %5.2fM %.2f\n",
exp(gmean_times/current_test),
exp(gmean_rate/current_test),
exp(gmean_ratio/current_test));
printf("\nTotal absolute time: %.2f sec\n", total_absolute_times);
// sam added 12/05/95
printf("\nAbstraction Penalty: %.2f\n\n", exp(gmean_ratio/current_test));
}
clock_t start_time, end_time;
inline void start_timer() { start_time = clock(); }
inline double timer() {
end_time = clock();
return (end_time - start_time)/double(CLOCKS_PER_SEC);
}
const double init_value = 3.;
double data[SIZE];
Double Data[SIZE];
inline void check(double result) {
if (result != SIZE * init_value)
printf("test %i failed\n", current_test);
}
void test0(double* first, double* last) {
start_timer();
for(int i = 0; i < iterations; ++i) {
double result = 0;
for (int n = 0; n < last - first; ++n) result += first[n];
check(result);
}
result_times[current_test++] = timer();
}
template <class Iterator, class T>
void test(Iterator first, Iterator last, T zero) {
int i;
start_timer();
for(i = 0; i < iterations; ++i)
check(double(accumulate(first, last, zero)));
result_times[current_test++] = timer();
}
template <class Iterator, class T>
void fill(Iterator first, Iterator last, T value) {
while (first != last) *first++ = value;
}
double d = 0.;
Double D = 0.;
typedef double* dp;
dp dpb = data;
dp dpe = data + SIZE;
typedef Double* Dp;
Dp Dpb = Data;
Dp Dpe = Data + SIZE;
typedef double_pointer dP;
dP dPb(dpb);
dP dPe(dpe);
typedef Double_pointer DP;
DP DPb(Dpb);
DP DPe(Dpe);
typedef reverse_iterator<dp, double> rdp;
rdp rdpb(dpe);
rdp rdpe(dpb);
typedef reverse_iterator<Dp, Double> rDp;
rDp rDpb(Dpe);
rDp rDpe(Dpb);
typedef reverse_iterator<dP, double> rdP;
rdP rdPb(dPe);
rdP rdPe(dPb);
typedef reverse_iterator<DP, Double> rDP;
rDP rDPb(DPe);
rDP rDPe(DPb);
typedef reverse_iterator<rdp, double> rrdp;
rrdp rrdpb(rdpe);
rrdp rrdpe(rdpb);
typedef reverse_iterator<rDp, Double> rrDp;
rrDp rrDpb(rDpe);
rrDp rrDpe(rDpb);
typedef reverse_iterator<rdP, double> rrdP;
rrdP rrdPb(rdPe);
rrdP rrdPe(rdPb);
typedef reverse_iterator<rDP, Double> rrDP;
rrDP rrDPb(rDPe);
rrDP rrDPe(rDPb);
int main(int argv, char** argc) {
if (argv > 1) iterations = atoi(argc[1]);
fill(dpb, dpe, double(init_value));
fill(Dpb, Dpe, Double(init_value));
test0(dpb, dpe);
test(dpb, dpe, d);
test(Dpb, Dpe, D);
test(dPb, dPe, d);
test(DPb, DPe, D);
test(rdpb, rdpe, d);
test(rDpb, rDpe, D);
test(rdPb, rdPe, d);
test(rDPb, rDPe, D);
test(rrdpb, rrdpe, d);
test(rrDpb, rrDpe, D);
test(rrdPb, rrdPe, d);
test(rrDPb, rrDPe, D);
summarize();
return 0;
}