1 / 22

CSE 303 Concepts and Tools for Software Development

CSE 303 Concepts and Tools for Software Development. Richard C. Davis UW CSE – 12/6/2006 Lecture 24 – Profilers. Administravia. HW7 is due today Short Papers are due Friday. Last Time. Wrap-up software engineering Readability Robustness. Today. Wrap-up C++ Generic Programming

aaron
Download Presentation

CSE 303 Concepts and Tools for Software Development

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. CSE 303Concepts and Tools for Software Development Richard C. DavisUW CSE – 12/6/2006 Lecture 24 – Profilers

  2. Administravia • HW7 is due today • Short Papers are due Friday CSE 303 Lecture 24

  3. Last Time • Wrap-up software engineering • Readability • Robustness CSE 303 Lecture 24

  4. Today • Wrap-up C++ • Generic Programming • Templates • The Standard Template Library • Wrap up Tools • Profilers CSE 303 Lecture 24

  5. Java Generics • Java without generics: less type checking ArrayList l = new ArrayList(); l.add(new Foo()); Foo f = (Foo)l.get(0); • Generics add type check and remove cast ArrayList<Foo> l = new ArrayList<Foo>(); l.add(new Foo()); Foo f = l.get(0); CSE 303 Lecture 24

  6. C++ Templates • Similar to Java generics in purpose • Can be used for: • Functions • Classes • Methods of classes (on newer compilers) • Question: How to cope without templates? • Answer: Use void pointers CSE 303 Lecture 24

  7. Template Syntax • For function declaration • Definitions similar • Class declarations/definitions similar template <typename Comparable> Comparable &findMax( vector<Comparable> &a); CSE 303 Lecture 24

  8. Templates Are a Giant Hairball! • Details vary from compiler to compiler • Standard is very hard to implement • Leaves some questions unanswered • Problems with separate compilation • Compiler creates implementation when needed • Should it go in a .cpp or a .h file? Not clear. • Warning: C++ templates vs. Java generics • Don't expect them to work the same way • They have rather different semantics CSE 303 Lecture 24

  9. Standard Template Library (STL) • Equivalent of Java Collections API • But uses templates • Strives for flexibility and speed • STL stores copies instead of references • Drawbacks • Same drawbacks as templates • Can really obfuscate your code • Note long compiler error messages CSE 303 Lecture 24

  10. That's all for Templates/STL • Want to learn more? • Your book is a good place to start • Practice! CSE 303 Lecture 24

  11. Profilers • Monitors running program • Reports detailed performance information • See where program uses most time/space • "90/10 rule of programs" • 90% of time spent in 10% of the code • Profilers help you find the 10% (bottlenecks) • Warning! Profilers can be misleading! CSE 303 Lecture 24

  12. Basic Features • Different profilers monitor different things • gprof: profiler for code produced by gcc • gprofis fairly typical and reports • # times each function was called • # times function "A" called function "B" • % time spent in each function • Estimated based on samples • Profiler periodically checks where program is CSE 303 Lecture 24

  13. Profiler Implementation Basics • Call counts • Add code to every function-call • Updates a table indexed by function pairs • The profiler instruments your code • Time samples • Use the processor's timer • Wake-up periodically • Check the value of the program counter CSE 303 Lecture 24

  14. Using gprof • Call gcc/g++ with option -pg • Both when compiling and linking • Compiler will add code for storing call count info • Linker will add functions for getting time samples • Execute the program to get file gmon.out • Run gprof to analyze results gprof profile-me gmon.out > results.txt • Examples: • primes-gprof.txt • polytest-gprof.txt & polytest-gprof-abbrev.txt CSE 303 Lecture 24

  15. Getting line-by-line results • Compile with option -g -pg • Run gprof with -l option gprof -l profile-me gmon.out > results.txt CSE 303 Lecture 24

  16. Profiler Gotchas • Profiler is sampling • Make sure there are enough samples • Program must run for a sufficiently long time • Results depend on your inputs • For instance, imagine array is already sorted • Programs will run very slow (~1/10 as fast) • Profiler won't report these times • The clock will! • Cumulative times based on estimation CSE 303 Lecture 24

  17. gprof Estimation Problem Example int g = 0; void c(int i) { if (i) return; for(; i<2000000000; ++i) g++; } void a() { c(0); } void b() { c(1); } int main() { a(); b(); return 0; } • See misleading.c & misleading-gprof.txt CSE 303 Lecture 24

  18. Poor man's Profiling with "time" • Use to measure whole program time • Usage example: time program args • Three types of time reported • real: roughly "wall clock" • Includes time used by other programs • Includes disk access time • user: time spent running code in the program • system: time the O/S spent doing stuff • Includes I/O (Note: gprof doesn't measure this) CSE 303 Lecture 24

  19. Performance Tuning • "First make it work, then make it fast" • Steps in performance tuning • Get things to work well, write clean code • Locate bottlenecks and performance issues • Sometimes they are obvious • Otherwise, profiler can help • Optimize your overall program • Use low-level tricks if you need to • Try compiling with "optimization flags" • gcc/g++ uses -O1, -O2, -O3 CSE 303 Lecture 24

  20. Summary • Profilers help us understand • Where program spends most time • What parts of code are executed most often • We use gprof, but many profilers exist • Visual Studio 2005 includes one • Similar goals and similar basic functionality • Implementation • Instrument the code • Perform extra work as program runs CSE 303 Lecture 24

  21. Reading • Optional readings • C++ for Java Programmers • Chapter 7: Templates • Chapter 10: STL • gprof manual • http://www.gnu.org/software/binutils/manual/gprof-2.9.1/gprof.html CSE 303 Lecture 24

  22. Next Time • Concurrency? • Wrap-up CSE 303 Lecture 24

More Related