Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Version 1 (modified by patrick, 15 years ago) (diff)

C++ Performance Tweaking Tips

Inline Functions

Function declared as inline will be included in the calling code in compilation time. This speeds up execution because all the branching stuff hasn't to be executed. The speedup will be the bettest, when these functions are only very small, so when the execution of the function needs aproximatly the same time as the branching time. Here a little example:

class Test
{
 public:
  int getSize();
 private:
  int size;
};

inline int Test:getSize()
{
  return this->size;
}

This function will be executed aproximatly 10 times faster on a Pentium II based processor. BUT Don't write everywhere inline functions: use it only for time critical stuff, so said functions, that are executed very often during the game-time of orxonox like:

  • void !WorldEntity::tick(float time) {} this function is called everytime a frame is rendered
  • bool !BaseObject::isFinalized() this function is called from the garbage collector every time he does its job

Don't use it for functions that are normaly called before and after the game time. Or functions that are rarely called at all.

Memory Allocation and Deletion: new, delete

Creation of new Objects needs very much time compared to mathematical operations. It can take you 20 to 200 times the time a normal function call coasts, depending of how much the class has been derived, etc (measured on a pentium II computer). So try to make as few new objects you can and recycle them if possible (altough it can lead to strang code).[br] Given the case, you have to create a new object, try to make it like this:

void ExampleClass::goodStyle()
{
  Object* obj = new Object();
  obj->doSomeStuf();
  delete obj;
}

To free the memory is very important, if the function is called multiple times! [br] If you write it in the follwing way, you don't have to delete it:

void ExampleClass::goodStyle2()
{
  Object obj;
  obj.doSomeMoreStuf();
}

The difference is, that this obj will be stored as a temporary variable and deleted after the function returns! This leads to some other problems: If you want to give a reference to object via the argument of a function to another object, never use these temporary variables.

void SomeClass::wantsObjectReference(Object* reference)
{
  /* do something with the reference */
}

void ExampleClass::badBadBad()
{
  Object obj;                      /* this is only a local reference! automaticly deleted after function return */
  SomeClass* sc = new SomeClass(); /* creation of a new object needs much time, avoid it if possible - here we need it */
  sc->wantObjectReference(&obj);   /* BAD BAD BAD BAD!!!!! */
  delete sc;
}

The compile will complain about such things with a message like this: "WARNING: taking address of a temprary". And Mr.Compiler is absolutly right! Better would be:

void SomeClass::wantsObjectReference(Object* reference)
{
  /* do something with the reference */
}

void ExampleClass::badBadBad()
{
  Object* obj = new Object*();     /* this is only a local reference! automaticly deleted after function return */
  SomeClass* sc = new SomeClass(); /* creation of a new object needs much time, avoid it if possible - here we need it */
  sc->wantObjectReference(obj);    /* BAD BAD BAD BAD!!!!! */
  delete sc;
}