i thought to replace std::vector
with std::array
in my program, so i went on testing:
template<typename T> class A{
public:
void sub_run(T w){
w[0] = 0.5;
w[1] = 1.5;
w[2] = 2.5;
w[3] = 0.0;
for (int i = 0; i < 100000000; i++)
{
w[0] = i *0.5;
w[1] = i *1.5;
w[2] = i *2.5;
w[3] = w[0] + w[1]*w[2];
}
}
};
int main(){
// Vectors
/*
A<vector<double> > inst_a;
vector<double> w(4);
inst_a.sub_run(w);
*/
// 1.71 sec
// C-array
/*
A<double *> inst_a;
double w[4];
inst_a.sub_run(w);
*/
// 1.03 sec
// std::array
A<array<double,4> > inst_a;
array<double,4> w;
inst_a.sub_run(w);
// 3.31 sec
return 0;
}
Well, the output is odd enough - the std::array
is slower than C-array in 3 times and even slower than vector
. No optimization was applied, the only flag is -std=c++11
.
What could be wrong?
From the GCC documentation about Optimization:
Without any optimization option, the compiler's goal is to reduce the cost of compilation and to make debugging produce the expected results. Statements are independent: if you stop the program with a breakpoint between statements, you can then assign a new value to any variable or change the program counter to any other statement in the function and get exactly the results you expect from the source code.
Without the -O option, GCC does not care about performance. It cares about debugging using gdb
or other similar tool. To actually compile programs that runs as fast as possible you should use the -O
options. For example, -O2
.