1 / 12

Introduction to Effective C++ Programming

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)

Download Presentation

Introduction to Effective C++ Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Effective C++ Programming Kwanghee Ko Design Laboratory Department of Ocean Engineering Massachusetts Institute of Technology Day 5

  2. 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.

  3. 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.

  4. 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(); }

  5. 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(); }

  6. 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(); }

  7. 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; }

  8. 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; }

  9. 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; }

  10. 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; }

  11. 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; }

  12. General Guidelines : Efficiency • Understand the costs of virtual functions.

More Related