Reputation: 119
I wrote header for arr class in arr.h with overloaded =
operator for 1D-array and checked for the time needed for
A=B
, where A
, B
arrays.
Time was comparable to the time in the simple loop for double
array (without class arr
) for optimization level less than 3.
#!/bin/bash
FLAG=$1
echo $FLAG
g++ -o main $FLAG -std=c++11 main.cpp -DARR && main
g++ -o main $FLAG -std=c++11 main.cpp && main
# end of the script
CMP -O0 ; CMP -O1 ; CMP -O2 ; CMP -O3 ; CMP -Ofast
gives
-O0
ARR
: 1.5790530000000000e+00 4.3474900000000000e-01 2.0138020000000001e+00ARR
: 4.0454800000000002e-01 4.4675500000000001e-01 8.5130300000000003e-01-O1
ARR
: 8.7386200000000003e-01 2.8413300000000002e-01 1.1579950000000001e+00ARR
: 3.3455699999999999e-01 3.5522599999999999e-01 6.8978300000000004e-01-O2
ARR
: 7.2507600000000005e-01 2.3888400000000001e-01 9.6396000000000004e-01ARR
: 3.3509400000000000e-01 3.5229899999999997e-01 6.8739300000000003e-01-O3
ARR
: 6.1693299999999995e-01 2.3233400000000001e-01 8.4926699999999999e-01ARR
: 2.3794000000000001e-01 1.9999999999999999e-06 2.3794199999999999e-01-Ofast
ARR
: 6.2396699999999994e-01 2.3364900000000000e-01 8.5761599999999993e-01ARR
: 2.3897499999999999e-01 1.9999999999999999e-06 2.3897699999999999e-01But for -O3
and -Ofast
simple loop demonstrates dramatic decrease of time (second column with numbers). How can I get the same result for arr
class?
main.cpp
#include <iostream>
#include <iomanip>
#include <time.h>
#ifdef ARR
#include "arr.h"
#endif
using namespace std;
int main() {
int n=1e8;
register int i;
clock_t time1=clock(),time2,time3=0;
#ifdef ARR
arr<double> A(n),B(n);
for (i=0; i<n; i++)
B(i)=i;
#else
double * A = new double [n];
double * B = new double [n];
for (i=0; i<n; i++)
B[i]=i;
#endif
time2=clock();
#ifdef ARR
A=B;
#else
for (i=0; i<n; i++)
A[i]=B[i];
#endif
time3=clock();
#ifdef ARR
cout<<fixed<<setw(16)<< scientific <<setprecision(16) <<" with ARR: "<<(time2-time1)/ (double)CLOCKS_PER_SEC<< " "<< (time3-time2)/ (double)CLOCKS_PER_SEC<< " "<< (time3-time1)/ (double)CLOCKS_PER_SEC<<endl;
#else
cout<<fixed<<setw(16)<< scientific <<setprecision(16)<<" without ARR: "<<(time2-time1)/ (double)CLOCKS_PER_SEC<< " "<< (time3-time2)/ (double)CLOCKS_PER_SEC<< " "<< (time3-time1)/ (double)CLOCKS_PER_SEC<<endl<<endl;
#endif
return 0;
}
arr.h
#ifndef ARR_H
#define ARR_H
using namespace std;
template <class T> class arr
{
public:
T* data;
int size, dim=0;
int dim1=0, dim2=0, dim3=0, dim4=0;
int shape[4];
arr() { }
arr(const int & isize) { // constructor
dim=1;
size=isize;
dim1=size;
data = new T[size];
shape[0]=size;
register int i;
for (i=0; i<size; i++)
data[i]=0.;
}
~arr() { delete [] data; }
T &operator()(const int & index) {
return data[index];
}
arr &operator=(const arr & A) {
register int i;
for (i=0; i<size; i++)
data[i]=A.data[i];
return *this;
}
arr &operator=(const T & A) {
register int i;
for (i=0; i<size; i++)
data[i]=A;
return *this;
}
};
#endif /*ARR_H */
Upvotes: 0
Views: 64
Reputation: 32732
Since you don't do anything with A
after copying the array, the optimizer is completely removing the loop. You need to use some value in the A
array (for example, output one element of the array in your cout
statement).
Upvotes: 1