Don't declare non-trivial temporary objects inside of heavily used
methods if you could instead keep a scratch object in the methods
class. For example, instead of:
class Class1 {
...
int f(int x) {
Class2 foobie;
foobie.reset();
foobie.doit(x);
foobie.doit(x);
return foobie.getstuff();
}
}
move foobie out of Class1::f into Class1:
class Class1 {
Class2 foobie;
...
int f(int x) {
foobie.reset();
foobie.doit(x);
foobie.doit(x);
return foobie.getstuff();
}
}
This gets rid of the constructor and destructor call
for foobie each time Class1::f is called.
This can be a really big savings, especially if Class2
allocates memory, etc.