C++ Optimization Tips
-
Upload
abdelrahman-al-ogail -
Category
Business
-
view
1.340 -
download
0
description
Transcript of C++ Optimization Tips
C++ Optimization Tips
Abdelrahman Al-Ogail05-12-2010
Agenda• Hello World
• Does it deserve?
• Initialization list
• Temporaries
• Memory pooling
• Inlining
Hello World
Software Performance
Design•Algorithms & Data Structures•Program Decomposition.
Coding•Language Constructs•System Architecture•Libraries•Compiler Optimizations
• What we mean by “fast”?
• When to think about optimization?
• What to optimize?– Fast path.
Does it deserve?
//******************* VERSION 0 *******************\\void MyFunc(){ string x = “1234568” ;}
void main(){ // Start timing here for (int i = 0; i < 1000000; i++) { MyFunc(); } // Stop timing here}
Does it deserve?
//******************* VERSION 1 *******************\\void MyFunc(){ char* x = “1234568” ;}
void main(){ // Start timing here for (int i = 0; i < 1000000; i++) { MyFunc(); } // Stop timing here}
Does it deserve?
Version 1 Version 00
50
100
150
200
250
300
350
Performance Analysis
Initialization list//******************* VERSION 0 *******************\\class MyClass{public:
MyClass(string s1, string s2, string s3) {_s1 = s1; _s2 = s2; _s3 = s3; }
private:string _s1, _s2, _s3;
};
void main(){ // Start timing here for (int i = 0; i < 1000000; i++) { MyClass instance("123456", "123456", "123456"); } // Stop timing here}
Initialization list//******************* VERSION 1 *******************\\class MyClass{public:
MyClass(string s1, string s2, string s3) : _s1(s1), _s2(s2), _s3(s3){}
private:string _s1, _s2, _s3;
};
void main(){ // Start timing here for (int i = 0; i < 1000000; i++) { MyClass instance("123456", "123456", "123456"); } // Stop timing here}
Initialization list
Version 1 Version 01600
1650
1700
1750
1800
1850
1900
1950
Performance Analysis
Temporaries
//******************* VERSION 0 *******************\\
void main(){ // Start timing here for (int i = 0; i < 1000000; i++) { string s1, s2, s3, s4, s5; s1 = s2 + s3 + s4 + s5; } // Stop timing here}
Temporaries//******************* VERSION 1 *******************\\
void main(){ // Start timing here for (int i = 0; i < 1000000; i++) { string s1, s2, s3, s4, s5; s1 = s2; s1 += s3; s1 += s4; s1 += s5; } // Stop timing here}
Temporaries
Version 1 Version 00
500
1000
1500
2000
2500
3000
Performance Analysis
Memory Pooling
• Size of memory to allocate:– Static.– Dynamic.
• Environment:– Single Threaded.– Multi-Threaded.
Quote of Wisdom
“Special circumstances allow for simplifying assumptions that in turn, provide
opportunities for significant optimizations”
Memory Pooling
void main(){ MyMemoryManager<MyClass> memManager; memManager.Reserve(10);
MyClass* instance = memManager.Alloc();
// Some code goes here…
// Deallocate the allocated instance memMnagaer.DeAlloc(instance); // Some code goes here…
memManager.Free();}
Memory Pooling
Default Memory Manager
Static/Single Memory Pool
Dynamic/Single0
200
400
600
800
1000
1200
1400
1600
Performance Analysis
Inlining
• Why Inlining?– Removes method invocation overhead.– Let compiler able to optimize code.
• Why Not Inlining:– Code expansion (page fault, exe image size).– Linkage problems.– Code recompilation.
Inlining
• Some of method invocation overhead*:– Passing method parameters– Creative stack frame for new call– Branching (IP)– Passing return values.
* architecture depended.
Inlining
• What to inline:– Singletons
– Trivial methods
– Fast path most frequent method calls.
Inlining – Conditional Inling
#if !defined(_X_H_)#define _X_H_class X { ... int y (int a ); ...};#if defined(INLINE)#include x.inl#endif#endif // _X_H_
#if !defined(INLINE)#define inline#endifinlineint X::y (int a){
...}
#if !defined(INLINE)#include x.inl#endif
x.h x.inl x.cpp
Inlining – Selective InlingFile: x.h:
class x {public: //assuming the original method "y" had public visibility
---int inline_y (int a);int y (int a); // this should
already have been here---
};#include "x.inl" // this may already have been here also
File: x.inl:inlineint x::inline_y (int a){
... // original implementation of y}
File: x.c:int x::y (int a){
return inline_y(a);}
Thanks!