Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changes between Version 3 and Version 4 of code/PerformanceTips


Ignore:
Timestamp:
Aug 19, 2005, 12:13:14 PM (19 years ago)
Author:
patrick
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • code/PerformanceTips

    v3 v4  
    44Function declared as inline will be included in the calling code in compilation time. This speeds up execution because all the branching stuff doesn't have to be executed. The speedup is maxed out, when these functions are only very small, so when the execution of the function needs approximatly the same time as the branching time. Here a little example:
    55{{{
     6// in test.h
    67class Test
    78{
    89 public:
    9   int getSize();
     10  int getSize() { return this->iSize; }
    1011 private:
    1112  int size;
    1213};
    1314
    14 inline int Test:getSize()
    15 {
    16   return this->size;
    17 }
     15/* remember that inline functions must be defined in the *.h (header) file! */
    1816}}}
     17Y
    1918This function will be executed aproximatly 10 times faster on a Pentium II based processor.
    2019'''BUT'''
    21 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:
    22  * ''void !WorldEntity::tick(float time) {}'' this function is called everytime a frame is rendered
    23  * ''bool !BaseObject::isFinalized()'' this function is called from the garbage collector every time he does its job
     20Don't write everywhere inline functions: use it only for :
     21 * small interface functions like {{{ int getAttribute {....};}}} or {{{ void setVelocity(float velocity) { this->velocity = velocity; } }}}
     22 * 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
    2423Don't use it for functions that are normally called before and after the game time or functions that are rarely called at all.
    25 Inlining brings some problems, too. First: Inlined code doesn't have to be made inline by the compiler. Some reasons, why this could happen are: loops in the inlined code, recursive code in the inlined code and function calls in the inlined code.
     24Inlining brings some problems, too. First: Inlined code doesn't have to be made inline by the compiler. Some reasons, why this could happen are: loops in the inlined code, recursive code in the inlined code and function calls in the inlined code. Private functions are inlined automaticaly.
    2625 
    2726== Memory Allocation and Deletion: new, delete ==
     
    3736}
    3837}}}
    39 To free the memory is very important, if the function is called multiple times! [br]
     38To free the memory is very important, if the function is called multiple times! But know, that deleting uses much time again. So if there is a possibliliy of reusing the old function. In time critical parts of the code (like in-game) you can think about creating the objects in initialisation time and delete them after the time-critical part.[br]
    4039If you write it the following way, you don't have to delete it:
    4140{{{
     
    6160}
    6261}}}
    63 The compile will complain about such things with a message like this: "WARNING: taking address of a temporary". And Mr. compiler is absolutly right!
     62The compiler will complain about such things with a message like this: "WARNING: taking address of a temporary". And Mr. compiler is absolutly right!
    6463A better way would be:
    6564{{{
     
    7372  Object* obj = new Object*();     /* this is only a local reference! automatically deleted after function return */
    7473  SomeClass* sc = new SomeClass(); /* creation of a new object needs much time, avoid it if possible - here we need it */
    75   sc->wantObjectReference(obj);    /* BAD BAD BAD BAD!!!!! */
    76   delete sc;
     74  sc->wantObjectReference(obj);   
     75  delete sc;                       /* remember that creating and deleting object need VERY much time*/
    7776}
    7877}}}