This time, I couldn’t find what I’m looking for (dunno if I’m not searching for the right stuff…) but, here it is:
In c++, imagine you have a function Bar() that is called once every cycle… like this:
class Foo {
public:
void Bar() {
double my_array[3];
// fills the array based on some calculations
double my_array1[3];
// fills the array based on some calculations
double my_array2[3];
// fills the array based on some calculations
_member_of_class = my_array + my_array1 + my_array2; //+ overloaded
}
private:
float _member_of_class[3];
}
int main(int argc, char** argv) {
Foo foo;
while(/*program running*/) {
foo.Bar();
//LOTS of code here
}
return 0;
}
Now, my_arrays are temporary arrays, not important to be data members, just used to fill the class member… Obviously, the overhead of calling that function is not necessary… Is there a way (well, I’m trying to avoid putting them as class members) of telling the compiler to “save the allocation space” or something so they is less overhead? const would give any tip to the compiler? I’m not sure I’m being clear…
Anyway, thanks!
Use a profiler
As it stands, the function is declared inline in a class. This code will trivially optimize, and the compiler will probably get the allocations out of the loop anyway (depending on how much cruft you have left out of the picture, really).
Also, the overloaded array operators likely get vectorized in gcc (starting with -O3 of -ftree-vectorize). Just look at the verbose out put with
to see what loops got vectorized, and if not, why not.
By all means have a look at the output of g++ -S etc.
Use a profiler. Don’t ‘optimize’ if you don’t know that it’s necessary.