09 generic programming and effective

37
Templates  The STL  C++ Without Bugs  Outlook 9. Generic Programming & Effective C++ June 17, 2010 9. Generic Programming & Effective C++ Introduction to C/C++,  Tobias Weinzierl  page 1 of 33

Upload: anidcohen9058

Post on 04-Jun-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 1/37

Templates   The STL   C++ Without Bugs   Outlook

9. Generic Programming & Effective C++

June 17, 2010 

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 1 of 33

Page 2: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 2/37

Templates   The STL   C++ Without Bugs   Outlook

Outline

•   Generic Programming: Templates

•   The Standard Template Library

•  (More) Effective C++—SomeExamples

•   Outlook

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 2 of 33

Page 3: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 3/37

Templates   The STL   C++ Without Bugs   Outlook

 —9.1. Templates —

•  Sometimes, it is cumbersome to write a feature for one type, as we know that we’llneed exactly the same feature for another type, too.

•  Standard example: Technical classes such as lists.

•  Study our linked list example. What, if we need this list for doubles and somestructs, too?

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 3 of 33

Page 4: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 4/37

Templates   The STL   C++ Without Bugs   Outlook

Generalisation of the Linked List With Respect to Its Type

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 4 of 33

Page 5: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 5/37

Templates   The STL   C++ Without Bugs   Outlook

Using Type-generic Classes

t e m p l a t e   <c l a s s T>c l as s L i n k e d Li s t   {

p u b l i c :v o i d   a p p e n d (T);

} ;

. . .L i n k e d L i s t<i n t>   m y I n t e g e r L i s t ;m y I n t e g e r L i s t . append ( 1 0 ) ;

L i n k e d L i s t<double>   myDoubleList ;m y Do u bl e Li s t . append ( 0 . 1 ) ;

m yD ou bl e Li s t = m y I n t e g e r L i s t ;   / / a r r r rg h  

•  Templates are classes which require a type to be instantiated.•  Then, we can use them as standard classes with the template argument replaced

by the concrete type.

•  If two template instantiations have different type, they are two completely differentclasses (similar to cut-n-paste).

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 5 of 33

Page 6: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 6/37

Templates   The STL   C++ Without Bugs   Outlook

Writing Type-generic Classes

t e m p l a t e   <c l a s s T>c l as s L i n k e d Li s t   {

p u b l i c :v o i d   append( co n st   T& v a l u e ) ;

} ;

t e m p l a t e   <c l a s s T>v o i d   L i n k e d L i s t<T>::append( co n st   T& value )   {

. . .}

•  Templates are defined solely in headers.

•  Templates are pain to debug.

•  However, it is, basically, only “‘replace all types by T”.

•  Usually, you will never have to write  templates. But you have to be able to  use them.

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 6 of 33

Page 7: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 7/37

Templates   The STL   C++ Without Bugs   Outlook

 —9.2. The STL —

t e m p l a t e   <c l a s s T>

c l a s s s t d : : v e c t o r   {. . .} ;

s t d : : v e c t o r<i n t>   myVector ;

myVector . push back ( 2 3 ) ; myVector . push back ( 24 ) ; myVector . push back ( 2 5) ;myVector . s iz e ( ) ;

m yV ec to r [ 1 ] = 1 7 ;m yV ec to r [ 0 ] = m yV ec to r [ 1 ] + m yV ec to r [ 2 ] ;

•  C++ comes along with a huge set of standard data structures.

•  These data structures all are templates.

•  Consequently, the standard library is called standard template library.

•  Among the most frequently used types are  std::string, std::list (dynamicdata structures), and std::vector (array which could change; however changingit is expensive).

•   See http://www.cppreference.com for a comprehensive overview.

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 7 of 33

Page 8: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 8/37

Templates   The STL   C++ Without Bugs   Outlook

The STL vector Class—Some of Its Operations

# i n c l u d e   <v e c t o r>

s t d : : v e c t o r<i n t>   myVector ;

assign assign (multiple) elements of a vectorat or [x] modify/return elements of the vectorclear removes all elements from the vector

empty returns true if the vector has no elementserase removes elements from a vectorinsert inserts elements into the vector (vector grows)pop back removes the last element of a vectorpush back appends an element to the end of the vectorsize returns the number of items in the vector. . . . . .

The vector is an abstraction of an array, as we can select individual elements in thesame way we can select elements from an array—with the [] operator. If we make thevector grow or shrink, C++ basically adopts the underlying data structure. However, thisis not efficient!

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 8 of 33

Page 9: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 9/37

Templates   The STL   C++ Without Bugs   Outlook

The STL list Class—Some of Its Operations

# i n c l u d e   < l i s t >

s td : : l i s t <i n t>   myList ;

assign assign (multiple) elements of a listclear removes all elements from the listempty returns true if the list is emptyerase removes elements from a list

insert inserts elements into the listpop back removes the last element of a listpush back appends an element to the end of the listpop front removes the first element of a listpush front sets a new first element for the listremove removes entriesreverse inverts the list

size returns the number of items in the list. . . . . .

The list’s signature is similar to the vector signature (consistency). The list is similar toour implementation of the (double) linked list. Inserting new elements, sorting the list,and inverting the whole sequence is rather cheap. However, accessing individualelements explicitly (random access ) is not what a list is made for.

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 9 of 33

Page 10: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 10/37

Templates   The STL   C++ Without Bugs   Outlook

The STL set Class—Some of Its Operations

# i n c l u d e   <se t>

s t d : : se t<i n t>   mySet ;

clear removes all elements from the setcount counts the elements in the listempty returns true if the set is empty

erase removes elements from a setinsert inserts elements into the setsize returns the number of items in the set. . . . . .

Again, the signature is similar to the other containers. The set can store any built-intype/struct/class for which the < operator and the ==   operator are defined.

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 10 of 33

Page 11: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 11/37

Templates   The STL   C++ Without Bugs   Outlook

The STL map Class—Some of Its Operations

# i n c l u d e   <map>

std : : map<i n t   , double>   myMap;

clear removes all elements from the mapcount(key) counts the elements in the mapempty returns true if the map is empty

erase(key) removes elements from a mapfind(key) searches for the key and returns an iterator(reference) to the caller

insert(key,value) inserts mapping (pair of key and value) into the mapsize returns the number of items in the map. . . . . .

A map maps entries of the first template type to the second type. Here, integers aremapped to doubles. However, you can plug-in any class you’d like as long as youprovide an equals operator  ==.

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 11 of 33

Page 12: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 12/37

Templates   The STL   C++ Without Bugs   Outlook

Loops over STL Containers

t e m p l a t e   <c l a s s T>

c l a s s s t d : : v e c t o r   {. . .

} ;

s t d : : v e c t o r<i n t>   myVector ;. . .f o r   ( i n t   i = 0; i<m yV ec to r . s i z e ( ) ; i + +)   {

 / / Do something w i t h my Vec tor [ i ] . For exampl e 

myVector [ i ] ++ ;}

•  What is the semantics of this code fragment?

•  For an iteration, the realisation (linked list, vector, etc.) does not matter.

•  This variant however works for vectors only.•  Even with a corresponding list operator, the performance would suffer.

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 12 of 33

Page 13: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 13/37

Templates   The STL   C++ Without Bugs   Outlook

The Iterator Concept

t e m p l a t e   <c l a s s T>

c l a s s s t d : : v e c t o r   {. . .

} ;

s t d : : v e c t o r<i n t>   myVector ;. . .s t d : : v e c t o r<i n t  >: : i t e r a t o r p = myV ect or . b eg in ( ) ;w h i l e   ( p ! = m yV ec to r . end ( ) )   {

∗p = . . . ;   / / myVector [ i ] = . . . ;  p ++;

}

•  The iterator is a class nested into the vector.

•  The iterator is used like a pointer is used.

•  The iterator hides how we proceed from one element to another.

•  There is some iterator-factory-methods such as begin() and end().

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 13 of 33

T l t Th STL C With t B O tl k

Page 14: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 14/37

Templates   The STL   C++ Without Bugs   Outlook

The Iterator Concept—the While Loop Rewritten

t e m p l a t e   <c l a s s T>

c l a s s s t d : : v e c t o r   {. . .

} ;

s t d : : v e c t o r<i n t>   myVector ;. . .f o r   (

s t d : : v e c t o r<i n t  >: : i t e r a t o r p = m yV ect or . b e gi n ( ) ; p ! = m yV ec to r . end ( ) ; p++

)   {∗p = . . . ;   / / myVector [ i ] = . . . ;  p ++;

}

•  The iterator is a class nested into the vector.

•  The iterator is used like a pointer is used.•  The iterator hides how we proceed from one element to another.

•  There is some iterator-factory-methods such as begin() and end().

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 14 of 33

Templates The STL C Without Bugs Outlook

Page 15: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 15/37

Templates   The STL   C++ Without Bugs   Outlook

The Const Iterator Concept

t e m p l a t e   <c l a s s T>

c l a s s s t d : : v e c t o r   {. . .

} ;

s t d : : v e c t o r<i n t>   myVector ;. . .f o r   (

s t d : : v e c t o r<i n t  >: : c o n s t i t e r a t o r p = myVector . b eg in ( ) ;

p != myVector .e nd ( ) ;p++

)   {∗p = . . . ;   / / myVector [ i ] = . . . ;  p ++;

}

•  The iterator is a class nested into the vector.•  The iterator is used like a pointer is used.

•  The iterator hides how we proceed from one element to another.

•  There is some iterator-factory-methods such as begin() and end().

•  Const iterators are not allowed to modify the traversed data structure.

•  Thus, they are significantly faster than a standard iterator.

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 15 of 33

Templates The STL C++ Without Bugs Outlook

Page 16: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 16/37

Templates   The STL   C++ Without Bugs   Outlook

Iterator Variants

•  Iterators hide the realisation of a sequence.

•  Each STL container provides iterators, i.e. we can chose a different realisation andour code still works efficiently.

•  There’s also

•   reverse iterators,•   const iterators, and•   reverse const iterators.

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 16 of 33

Templates The STL C++ Without Bugs Outlook

Page 17: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 17/37

Templates   The STL   C++ Without Bugs   Outlook

 —9.3. C++ Without Bugs —

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 17 of 33

Templates The STL C++ Without Bugs Outlook

Page 18: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 18/37

Templates   The STL   C++ Without Bugs   Outlook

Virtual Destructors

cl ass EnemyTarget   {

p u b l i c :EnemyTarget ( )   {   ++numTargets ;   }˜EnemyTarget ()   { −−numTargets ;   }

p r i v a t e :s t a t i c i n t   numTargets ;

} ;

 / / i n t h e cpp f i l e 

i n t   EnemyTarget : : numTargets = 0;

Example from “Effective C++”.

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 18 of 33

Templates   The STL   C++ Without Bugs   Outlook

Page 19: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 19/37

p g

Virtual Destructors

cl ass EnemyTarget   {

p u b l i c :EnemyTarget ( )   {   ++numTargets ;   }˜EnemyTarget ()   { −−numTargets ;   }

p r i v a t e :s t a t i c i n t   numTargets ;

} ;

cl ass EnemyTank : pu bl i c EnemyTarget   {

p u b l i c :EnemyTank ( )   {}˜ EnemyTank ( )   {}

} ;

. . .

EnemyTarget∗   newTarget = new EnemyTank ( ) ;

d e l e te n ewTarg et ;

Example from “Effective C++”.

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 19 of 33

Page 20: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 20/37

Templates   The STL   C++ Without Bugs   Outlook

Page 21: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 21/37

References vs. Copies as Result Values

c l as s R a t i on a l   {

p r i v a t e :i n t   numerato r ;i n t   denomin ator ;

p u b l i c :R a t i on a l ( ) ;

co n st   R a t i o n a l& m u l t i p l y W i t h (   co n st   Re a ti o n a l& a no th erNu mb er )   co n st   {R a t i o n a l ∗   r e s u l t = new R a t i o n a l ( . . . ) ;

r e t u r n   ∗ r e s u l t ;}

} ;

R a t i o n a l a , b ; / / do something w i t h a and b R at i on al c = a . m u l t ip l y W i th ( b ) ;

Example from “Effective C++”.

•  Here, the whole Rational object is copied, and copying is time-consuming.

•  Reference make the memory handling easier (than with pointers). However, it’ssometimes still tricky. Here, you’ve created a memory leak.

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 21 of 33

Templates   The STL   C++ Without Bugs   Outlook

Page 22: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 22/37

Return a Copy

c l as s R a t i on a l   {

p r i v a t e :i n t   numerato r ;i n t   denomin ator ;

p u b l i c :R a t i on a l ( ) ;

R a t i on a l m u l t i p l y W i t h (   co n st   Re a ti o n a l& a no th erNu mb er )   co n st   {R a t i o n a l r e s u l t ( . . . ) ;

r e t u r n   r e s u l t ;

 / / or , even b e t t e r : r e t u r n   R at i on al ( . . . ) ;

}} ;

R a t i o n a l a , b ; / / do something w i t h a and b R at i on al c = a . m u l t ip l y W i th ( b ) ;

Example from “Effective C++”.

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 22 of 33

Templates   The STL   C++ Without Bugs   Outlook

Page 23: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 23/37

Never Redefine Default Parameters

enum   ShapeColour   {Red, Green , Blue } ;

cl ass Shape   {p u b l i c :

v i r t u a l   v o i d   d ra w ( S ha pe Co lo ur c o l o u r = R ed )   co n st   = 0 ;} ;

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 23 of 33

Templates   The STL   C++ Without Bugs   Outlook

Page 24: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 24/37

Never Redefine Default Parameters

enum   ShapeColour   {Red, Green , Blue } ;

cl ass Shape   {p u b l i c :

v i r t u a l   v o i d   d ra w ( S ha pe Co lo ur c o l o u r = R ed )   co n st   = 0 ;} ;

c l a s s R ec t an gl e : p u b l i c Shape   {p u b l i c :v i r t u a l   v o i d   d ra w ( S ha pe Co lo ur c o l o u r = Green )   co n st   = 0 ;

} ;

Shape∗   p r = new Re cta n gle ;p r   −>   d raw ( ) ;   / / what i s t he c ol o ur ?  

Example from “Effective C++”.

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 23 of 33

Templates   The STL   C++ Without Bugs   Outlook

Page 25: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 25/37

Never Redefine Default Parameters

enum   ShapeColour   {Red, Green , Blue } ;

cl ass Shape   {p u b l i c :

v i r t u a l   v o i d   d ra w ( S ha pe Co lo ur c o l o u r = R ed )   co n st   = 0 ;} ;

c l a s s R ec t an gl e : p u b l i c Shape   {p u b l i c :v i r t u a l   v o i d   d ra w ( S ha pe Co lo ur c o l o u r = Green )   co n st   = 0 ;

} ;

Shape∗   p r = new Re cta n gle ;p r   −>   d raw ( ) ;   / / what i s t he c ol o ur ?  

Example from “Effective C++”.

•  pr has a dynamic type which is different from its static type.

•  Default parameters are defined statically.

•  Who defined this behaviour—we don’t know!

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 23 of 33

Page 26: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 26/37

Templates   The STL   C++ Without Bugs   Outlook

Page 27: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 27/37

Combine C and C++

•  Combining C and C++ is easy if you can recompile the application with a C++

compiler.•  Name mangling: Translate C++ names into symbols.

•  C and C++ byte codes are not compatible.

•  Thus, write headers for your C libraries with

e x t e r n   ”C”

v o i d   foo ( i n t   a ,   i n t   b ) ;

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 25 of 33

Templates   The STL   C++ Without Bugs   Outlook

Page 28: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 28/37

 —9.4. Outlook —

•  Exceptions & Error Handling

•   Debugging

•   Integrated Development Environments

•  Architectural Patterns & DesignPatterns

•   Performance Analysis

•  From the Waterfall Model to AgileMethods

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 26 of 33

Templates   The STL   C++ Without Bugs   Outlook

Page 29: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 29/37

Exceptions and Error Handling

v o i d   A : : b ar ( )   {

. . . / / he re , we do a d i v i s i o n by zero o r 

throw MyException ;   / / i t ’ s a c l as s we ’ v e d e fi n ed b ef o re and   / / we mi ght pa ss i t many f l a g s d e s c r i b i n g  / / t h e e r r o r 

}

v o i d   A : : f o o ( )   {t r y   {

. . .b ar ( ) ;. . .

}c a t c h ( M yE xc ep ti on& e x c e p t i o n )   {

 / / r o l l ba ck a p p l i c a t i o n t o a v a l i d s t a t e }

}

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 27 of 33

Templates   The STL   C++ Without Bugs   Outlook

Page 30: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 30/37

Debugging

Our bug searching strategy:

9. Generic Programming & Effective C++

Introduction to C/C++, Tobias Weinzierl   page 28 of 33

Templates   The STL   C++ Without Bugs   Outlook

Page 31: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 31/37

Debugging

Our bug searching strategy:

s t d : : c ou t   <<   ” v a l ue o f s om et hi ng ” ;

•   Debuggers  are programs where you can define breakpoints .

•   Debuggers  allow you to study the values of all variables then.

•   Debuggers  support single step execution.

•   Debuggers  sometimes even allow you to modify variables manually.

9. Generic Programming & Effective C++Introduction to C/C++, Tobias Weinzierl   page 28 of 33

Templates   The STL   C++ Without Bugs   Outlook

Page 32: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 32/37

Integrated Development Environments

9. Generic Programming & Effective C++Introduction to C/C++, Tobias Weinzierl   page 29 of 33

Templates   The STL   C++ Without Bugs   Outlook

Page 33: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 33/37

Architectural Patterns & Design Patterns

. . . or what is a three tier architecture?

•  Read a nice book.•   Study http://martinfowler.com/eaaCatalog/.

•  Use other’s software—in particular frameworks and libraries (such as Peano,Dune, deal.ii).

9. Generic Programming & Effective C++Introduction to C/C++, Tobias Weinzierl   page 30 of 33

Templates   The STL   C++ Without Bugs   Outlook

Page 34: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 34/37

Performance Analysis

The simplest tools is gprof , but there’s tons of such software around.

Each sample counts as 0.01 seconds.% cumulative self self total

time seconds seconds calls ms/call ms/call name

33.34 0.02 0.02 7208 0.00 0.00 open

16.67 0.03 0.01 244 0.04 0.12 offtime

16.67 0.04 0.01 8 1.25 1.25 memccpy

16.67 0.05 0.01 7 1.43 1.43 write

16.67 0.06 0.01 mcount0.00 0.06 0.00 236 0.00 0.00 tzset

0.00 0.06 0.00 192 0.00 0.00 tolower

0.00 0.06 0.00 47 0.00 0.00 strlen

0.00 0.06 0.00 45 0.00 0.00 strchr

0.00 0.06 0.00 1 0.00 50.00 main

0.00 0.06 0.00 1 0.00 0.00 memcpy

0.00 0.06 0.00 1 0.00 10.11 print0.00 0.06 0.00 1 0.00 0.00 profil

0.00 0.06 0.00 1 0.00 50.00 report

9. Generic Programming & Effective C++Introduction to C/C++, Tobias Weinzierl   page 31 of 33

Templates   The STL   C++ Without Bugs   Outlook

Page 35: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 35/37

From the Waterfall Model to Agile Methods

9. Generic Programming & Effective C++Introduction to C/C++, Tobias Weinzierl   page 32 of 33

Templates   The STL   C++ Without Bugs   Outlook

Page 36: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 36/37

How to Continue

•  Read the “Effective” Books•  Read the Design Patterns by the Gang of Four

•  Read the book Refactoring from Martin Fowler(if you have to use other’s (old) code)

•  Read the book Write Great Code(if you want to understand how and while code performs the way it does)

•  Attend one of the LRZ courses(http://www.lrz-muenchen.de/services/compute/courses/)(the HPC courses are free for students from all over Germany)

•  GPGPU Programming•  Visualisation of Large Data Sets•  Compact course: Iterative linear solvers and parallelization

•   Parallel Programming of High Performance Systems•   . . .

9. Generic Programming & Effective C++Introduction to C/C++, Tobias Weinzierl   page 33 of 33

Templates   The STL   C++ Without Bugs   Outlook

Page 37: 09 Generic Programming and Effective

8/13/2019 09 Generic Programming and Effective

http://slidepdf.com/reader/full/09-generic-programming-and-effective 37/37

How to Continue

•  Read the “Effective” Books•  Read the Design Patterns by the Gang of Four

•  Read the book Refactoring from Martin Fowler(if you have to use other’s (old) code)

•  Read the book Write Great Code(if you want to understand how and while code performs the way it does)

•  Attend one of the LRZ courses(http://www.lrz-muenchen.de/services/compute/courses/)(the HPC courses are free for students from all over Germany)

•  GPGPU Programming•  Visualisation of Large Data Sets•  Compact course: Iterative linear solvers and parallelization

•   Parallel Programming of High Performance Systems•   . . .

•   . . . write me an email

9. Generic Programming & Effective C++Introduction to C/C++, Tobias Weinzierl   page 33 of 33