+ All Categories
Home > Documents > CSE 303 Concepts and Tools for Software Development

CSE 303 Concepts and Tools for Software Development

Date post: 27-Jan-2016
Category:
Upload: aaron
View: 38 times
Download: 3 times
Share this document with a friend
Description:
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 - PowerPoint PPT Presentation
22
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 12/6/2006 Lecture 24 – Profilers
Transcript
Page 1: CSE 303 Concepts and Tools for  Software Development

CSE 303Concepts and Tools for Software Development

Richard C. DavisUW CSE – 12/6/2006

Lecture 24 – Profilers

Page 2: CSE 303 Concepts and Tools for  Software Development

12/6/2006 CSE 303 Lecture 24 2

Administravia

• HW7 is due today

• Short Papers are due Friday

Page 3: CSE 303 Concepts and Tools for  Software Development

12/6/2006 CSE 303 Lecture 24 3

Last Time

• Wrap-up software engineering– Readability– Robustness

Page 4: CSE 303 Concepts and Tools for  Software Development

12/6/2006 CSE 303 Lecture 24 4

Today

• Wrap-up C++– Generic Programming– Templates– The Standard Template Library

• Wrap up Tools– Profilers

Page 5: CSE 303 Concepts and Tools for  Software Development

12/6/2006 CSE 303 Lecture 24 5

Java Generics

• Java without generics: less type checkingArrayList l = new ArrayList();

l.add(new Foo());

Foo f = (Foo)l.get(0);

• Generics add type check and remove castArrayList<Foo> l = new ArrayList<Foo>();

l.add(new Foo());

Foo f = l.get(0);

Page 6: CSE 303 Concepts and Tools for  Software Development

12/6/2006 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

Page 7: CSE 303 Concepts and Tools for  Software Development

12/6/2006 CSE 303 Lecture 24 7

Template Syntax

• For function declaration– Definitions similar– Class declarations/definitions similar

template <typename Comparable>

Comparable &findMax( vector<Comparable> &a);

Page 8: CSE 303 Concepts and Tools for  Software Development

12/6/2006 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

Page 9: CSE 303 Concepts and Tools for  Software Development

12/6/2006 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

Page 10: CSE 303 Concepts and Tools for  Software Development

12/6/2006 CSE 303 Lecture 24 10

That's all for Templates/STL

• Want to learn more?– Your book is a good place to start– Practice!

Page 11: CSE 303 Concepts and Tools for  Software Development

12/6/2006 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!

Page 12: CSE 303 Concepts and Tools for  Software Development

12/6/2006 CSE 303 Lecture 24 12

Basic Features

• Different profilers monitor different things• gprof: profiler for code produced by gcc• gprof is 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

Page 13: CSE 303 Concepts and Tools for  Software Development

12/6/2006 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

Page 14: CSE 303 Concepts and Tools for  Software Development

12/6/2006 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

Page 15: CSE 303 Concepts and Tools for  Software Development

12/6/2006 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

Page 16: CSE 303 Concepts and Tools for  Software Development

12/6/2006 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

Page 17: CSE 303 Concepts and Tools for  Software Development

12/6/2006 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

Page 18: CSE 303 Concepts and Tools for  Software Development

12/6/2006 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)

Page 19: CSE 303 Concepts and Tools for  Software Development

12/6/2006 CSE 303 Lecture 24 19

Performance Tuning

• "First make it work, then make it fast"• Steps in performance tuning

1. Get things to work well, write clean code

2. Locate bottlenecks and performance issues– Sometimes they are obvious– Otherwise, profiler can help

3. Optimize your overall program

4. Use low-level tricks if you need to

• Try compiling with "optimization flags"– gcc/g++ uses -O1, -O2, -O3

Page 20: CSE 303 Concepts and Tools for  Software Development

12/6/2006 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

Page 21: CSE 303 Concepts and Tools for  Software Development

12/6/2006 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

Page 22: CSE 303 Concepts and Tools for  Software Development

12/6/2006 CSE 303 Lecture 24 22

Next Time

• Concurrency?

• Wrap-up


Recommended