120 likes | 244 Views
Introduction to Effective C++ Programming. Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 5. General Guidelines : Efficiency. Don’t guess. Use an execution profiler to isolate performance problems. (80-20 rules)
E N D
Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 5
General Guidelines : Efficiency • Don’t guess. Use an execution profiler to isolate performance problems. (80-20 rules) • g++ : use ‘–pg’. Then use ‘gprof’. • Optimize your code as much as you can at a source level. • Optimization options (‘-O2’ in g++) provided by a compiler could be another choice. But it has limitation.
General Guidelines : Efficiency • Consider using lazy evaluation. • Defer computations until the results of those computations are required. • Understand the origin of temporary objects. • Implicit type conversion • Object return from a function • Overload to avoid implicit type conversions. • Facilitate the return value optimization. • Consider using ‘op=’ instead of stand-alone ‘op’. • Provide both in your class. • Consider alternative libraries.
Efficiency : Example • First Design • Number of objects created : 15000001 • Elapsed Time : 1.3 sec. int main(void) { Test_Int a(102); a.report(); cout << a << endl; for(int i=0;i<500000;i++) for(int j=0;j<10;j++) a = a + 1; a.report(); cout << a << endl; a.report(); }
Efficiency : Example • Second Design • Number of objects created : • Elapsed Time : int main(void) { Test_Int a(102); a.report(); const Test_Int one=1; cout << a << endl; for(int i=0;i<500000;i++) for(int j=0;j<10;j++) a = a + one; a.report(); cout << a << endl; a.report(); }
Efficiency : Example • Second Design • Number of objects created : 10000002 (67%) • Elapsed Time : 0.74 sec. (57%) int main(void) { Test_Int a(102); a.report(); const Test_Int one=1; cout << a << endl; for(int i=0;i<500000;i++) for(int j=0;j<10;j++) a = a + one; a.report(); cout << a << endl; a.report(); }
Efficiency : Example • Third Design • Number of objects created : • Elapsed Time : int main(void) { Test_Int a(102); a.report(); const Test_Int one=1; cout << a << endl; for(int i=0;i<500000;i++) for(int j=0;j<10;j++) a += one; a.report(); cout << a << endl; a.report(); } Test_Int Test_Int::operator+=(const Test_Int& t) { return a+=t.a; }
Efficiency : Example • Third Design • Number of objects created : 5000000 (57%) • Elapsed Time : 0.43 sec. (58%) int main(void) { Test_Int a(102); a.report(); const Test_Int one=1; cout << a << endl; for(int i=0;i<500000;i++) for(int j=0;j<10;j++) a += one; a.report(); cout << a << endl; a.report(); } Test_Int Test_Int::operator+=(const Test_Int& t) { return a+=t.a; }
Efficiency : Example • Fourth Design • Number of objects created : • Elapsed Time : Test_Int& Test_Int::operator+=(const Test_Int& t) { add(t); return *this; } void Test_Int::add(const Test_Int& t) { a += t.a; }
Efficiency : Example • Fourth Design • Number of objects created : 2 (0.00004%) • Elapsed Time : 0.3 sec. (70%) Test_Int& Test_Int::operator+=(const Test_Int& t) { add(t); return *this; } void Test_Int::add(const Test_Int& t) { a += t.a; }
Efficiency : Example • Fourth Design (relative to the first design) • Number of objects created : Almost nothing. • Elapsed Time : 23% Test_Int& Test_Int::operator+=(const Test_Int& t) { add(t); return *this; } void Test_Int::add(const Test_Int& t) { a += t.a; }
General Guidelines : Efficiency • Understand the costs of virtual functions.