Changes between Version 3 and Version 4 of code/PerformanceTips
- Timestamp:
- Aug 19, 2005, 12:13:14 PM (19 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
code/PerformanceTips
v3 v4 4 4 Function 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: 5 5 {{{ 6 // in test.h 6 7 class Test 7 8 { 8 9 public: 9 int getSize() ;10 int getSize() { return this->iSize; } 10 11 private: 11 12 int size; 12 13 }; 13 14 14 inline int Test:getSize() 15 { 16 return this->size; 17 } 15 /* remember that inline functions must be defined in the *.h (header) file! */ 18 16 }}} 17 Y 19 18 This function will be executed aproximatly 10 times faster on a Pentium II based processor. 20 19 '''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 rendered23 * ''bool !BaseObject::isFinalized()'' this function is called from the garbage collector every time he does its job20 Don'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 24 23 Don'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. 24 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. Private functions are inlined automaticaly. 26 25 27 26 == Memory Allocation and Deletion: new, delete == … … 37 36 } 38 37 }}} 39 To free the memory is very important, if the function is called multiple times! [br]38 To 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] 40 39 If you write it the following way, you don't have to delete it: 41 40 {{{ … … 61 60 } 62 61 }}} 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!62 The compiler will complain about such things with a message like this: "WARNING: taking address of a temporary". And Mr. compiler is absolutly right! 64 63 A better way would be: 65 64 {{{ … … 73 72 Object* obj = new Object*(); /* this is only a local reference! automatically deleted after function return */ 74 73 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*/ 77 76 } 78 77 }}}