optimization and maintainability - saifr · optimisation and maintainability, david grellscheid...
TRANSCRIPT
![Page 1: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/1.jpg)
Optimization and Maintainability
David Grellscheid
![Page 2: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/2.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Typical scientific workflowCorrectness is main
concern
Start coding without much planning
First version that looks like it works is kept
Sub-optimal choices only noticed later on
(if at all)
What writing scientific codes looks like…
• Many of us write programs to solve specific problems in science• We create and use models to
describe our problems• These models are implemented
as code and produce results• Evaluating these results allows
us to validate our models and improve them
Physical Problem
Model
Implementation
Evaluation
Refinements
![Page 3: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/3.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Typical scientific workflowCorrectness is main
concern
Start coding without much planning
First version that looks like it works is kept
Sub-optimal choices only noticed later on
(if at all)
What writing scientific codes looks like…
• Many of us write programs to solve specific problems in science• We create and use models to
describe our problems• These models are implemented
as code and produce results• Evaluating these results allows
us to validate our models and improve them
Physical Problem
Model
Implementation
Evaluation
Refinements
A friend of my friend said that
you should never do XYZ, because the code will be slower!
![Page 4: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/4.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Donald Knuth, December 1974:
Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.
“Structured Programming with go to Statements”, Computing Surveys, Vol 6, No 4.
![Page 5: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/5.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Runtime is not the only factor to consider,need to think about trade off between time spent in:
developmentdebuggingvalidationportability
runtime in your own usageother developers’ time (now/future)
total runtime for all users
![Page 6: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/6.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Runtime is not the only factor to consider,need to think about trade off between time spent in:
developmentdebuggingvalidationportability
runtime in your own usageother developers’ time (now/future)
total runtime for all users
CPU time much cheaper than human time!
![Page 7: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/7.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Optimization pointsSomeone else already solved (part of) the problem:
LAPACK, BLASGNU scientific library
C++ BoostNumpy, Scipy, Pandas
…
Develop googling skills, evaluate what exists. Quality often much better than self-written attempts
![Page 8: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/8.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Optimization points
Choice of programming language
Be aware of what exists
Know strengths / weaknesses
But: needs to fit rest of project
take a look at Haskell, Erlang, JS
![Page 9: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/9.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Optimization points
findLongestUpTo :: Int -> (Int,Int) findLongestUpTo mx = maximum ( map f [1 .. mx] ) where f x = (collatzLength x,x)
collatzLength :: Int -> Int collatzLength 1 = 1 collatzLength n = 1 + collatzLength (collatzStep n)
collatzStep :: Int -> Int collatzStep n | even n = n `div` 2 | otherwise = 3 * n + 1
![Page 10: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/10.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Optimization points
Program design
First version: understand the problems
start again
Second version: you know what you’re doing
refactor / clean up / make reusable
Done :-)
![Page 11: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/11.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Optimization points
Algorithm / data structure choice
can get orders of magnitude in speed
Local and hardware-specific optimisations
- later lecture -
![Page 12: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/12.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Complexity basics
Much simplified, skipping formal derivation
![Page 13: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/13.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Complexity basics
Much simplified, skipping formal derivation
while not is_sorted(xs): random.shuffle(xs)
![Page 14: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/14.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Complexity basics
Much simplified, skipping formal derivation
while not is_sorted(xs): random.shuffle(xs)
Scaling behaviour with size N of problem set:O(1) - constant time independent of NO(N) - linear with NO(N2) - quadratic in N
![Page 15: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/15.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Complexity basics
Much simplified, skipping formal derivation
while not is_sorted(xs): random.shuffle(xs)
Scaling behaviour with size N of problem set:O(1) - constant time independent of NO(N) - linear with NO(N2) - quadratic in N
O(N N!)
![Page 16: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/16.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Merge Sort
2 7 5 1 4 3 6 8
2 7 5 1 4 3 6 8
2 7 5 1 4 3 6 8
2 7 5 1 4 3 6 8
![Page 17: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/17.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Merge Sort
2 7 5 1 4 3 6 8
2 7 5 1 4 3 6 8
2 7 5 1 4 3 6 8
2 7 5 1 4 3 6 8
![Page 18: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/18.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Merge Sort
2 7 5 1 4 3 6 8
2 7 5 1 4 3 6 8
2 7 5 1 4 3 6 8
2 7 5 1 4 3 6 8
![Page 19: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/19.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Merge Sort
2 7 5 1 4 3 6 8
2 7 5 1 4 3 6 8
2 7 5 1 4 3 6 8
2 7 5 1 4 3 6 8
merge
![Page 20: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/20.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Merge Sort
2 7 5 1 4 3 6 8
2 7 5 1 4 3 6 8
2 7 1 5 4 3 6 8
2 7 5 1 4 3 6 8
merge merge
![Page 21: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/21.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Merge Sort
2 7 5 1 4 3 6 8
1 2 5 7 4 3 6 8
2 7 1 5 4 3 6 8
2 7 5 1 4 3 6 8
merge merge
merge
![Page 22: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/22.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Merge Sort
2 7 5 1 4 3 6 8
1 2 5 7 4 3 6 8
2 7 1 5 3 4 6 8
2 7 5 1 4 3 6 8
merge merge
merge
merge
![Page 23: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/23.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Merge Sort
2 7 5 1 4 3 6 8
1 2 5 7 4 3 6 8
2 7 1 5 3 4 6 8
2 7 5 1 4 3 6 8
merge merge
merge
merge merge
![Page 24: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/24.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Merge Sort
2 7 5 1 4 3 6 8
1 2 5 7 3 4 6 8
2 7 1 5 3 4 6 8
2 7 5 1 4 3 6 8
merge merge
merge
merge merge
merge
![Page 25: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/25.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Merge Sort
1 2 3 4 5 6 7 8
1 2 5 7 3 4 6 8
2 7 1 5 3 4 6 8
2 7 5 1 4 3 6 8
merge merge
merge
merge merge
merge
merge
![Page 26: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/26.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Merge Sort
1 2 3 4 5 6 7 8
1 2 5 7 3 4 6 8
2 7 1 5 3 4 6 8
2 7 5 1 4 3 6 8
merge merge
merge
merge merge
merge
merge
O(N log N)
![Page 27: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/27.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Merge Sort
1 2 3 4 5 6 7 8
1 2 5 7 3 4 6 8
2 7 1 5 3 4 6 8
2 7 5 1 4 3 6 8
merge merge
merge
merge merge
merge
merge
O(N log N)
15 Sorting Algorithms in 6 Minutes http://youtu.be/kPRA0W1kECg
![Page 28: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/28.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
http://bigocheatsheet.com/
Nicolai Josuttis, The C++ Standard Library.
![Page 29: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/29.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Data structure complexity
![Page 30: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/30.jpg)
Data Structures
10 2 7 5 1 4 9 3 6 8
Sequence Associative
5 Alpha
key value
3 Beta
7 Gamma
e.g., C-Arrays, std::vector, std::deque, std::list e.g., C-Arrays, std::map, std::set, std::unordered_map
![Page 31: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/31.jpg)
Data Structures
• Operations:• Insertion• Searching• Deletion
• Variants:• Ordered• Unordered
![Page 32: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/32.jpg)
Sequential ContainersArrays, Lists, Queues, Stacks
10 2 7 5 1 4 9 3 6 8
10 2 5 7 8 6
![Page 33: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/33.jpg)
STL Containers
• Sequence Containers• vector (flexible sequence)• deque (double-ended queue)• list (double linked list)• array (fixed sequence, C++11)• forward_list (single linked list, C++11)
![Page 34: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/34.jpg)
C-Arrays
• Simplest Sequence Data Structure
• Data stored in range [0, numElements)
• Fixed Size, Wasteful
• Consecutive Memory (efficient access)
10 2 7 5 1 4 9 3
int a[10000];int numElements = 0;
// insertion at end O(1)a[numElements++] = new_value;
// insertion at beginning O(n)for(int i = numElements; i > 0; i--) a[i] = a[i-1];a[0] = new_value;numElements++;
Inserting at end is O(1)
5
5 10 2 7 5 1 4 9
Therefore inserting at beginning is O(n)
3
O(n) copy of previous values to new location
10 2 7 5 5 1 4 9 3
O(n) copy
inserting in the middle is O(n)
![Page 35: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/35.jpg)
std::vector#include <iostream>#include <vector>
using namespace std;
// empty constructionvector<int> a;// sized constructionvector<int> a(10);// sized construction with initial valuevector<int> a(100, -1);// C++ 11 initializer listsvector<int> a { 3, 5, 7, 9, 11 };
// insertion at enda.push_back(3);a.push_back(5);a.push_back(7);
// delete at enda.pop_back();
// insertion at beginninga.insert(a.begin(), new_value);
// accessing elements just like arraysfor(int i = 0; i < a.size(); i++) {
cout << a[i] << endl;}
// using iteratorsfor(auto i = a.begin(); i != a.end(); ++i) {
cout << *i << endl;}
// C++11 for eachfor(auto element : a) {
cout << element << endl;}
![Page 36: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/36.jpg)
Linked-List
• List Elements connected through pointers• First Element (head) and last element (tail)
are always known• Insertion/Deletion at both ends in O(1)• Insertion in the middle is also cheaper
• Finding insertion location is O(n) compared to O(1) with C-Arrays
• But insertion itself happens in O(1) instead of O(n) copies
• Dynamic Size• Distributed in memory
10 2 5
head
10 2 5
head
struct Node {Node * prev;Node * next;int data;
}
Single Linked-List:only pointer of next element
Double Linked-List: pointer of previous and next element
7
tail
7
tail
![Page 37: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/37.jpg)
std::list#include <iostream>#include <list>
using namespace std;
// empty constructionlist<int> a;// sized constructionlist<int> a(10);// sized construction with initial valuelist<int> a(100, -1);// C++ 11 initializer listslist<int> a { 3, 5, 7, 9, 11 };
// insertion at beginninga.push_front(3);
// insertion at enda.push_back(3);
// delete at beginninga.pop_front();
// delete at enda.pop_back();
// access front elementint first = a.front();
// access last elementint last = a.back();
// using iteratorsfor(auto i = a.begin(); i != a.end(); ++i) {
cout << *i << endl;}
// C++11 for eachfor(auto element : a) {
cout << element << endl;}
![Page 38: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/38.jpg)
Queue
• First-In-First-Out (FIFO) data structure
• Implementations:• Double-Linked-List
• Operations:• enqueue: put element in queue (insert at tail)• dequeue: get first element in queue (remove head)
head
tail
enqueue
tail
head
dequeue
![Page 39: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/39.jpg)
Stack
• Last-In-First-Out (LIFO) data structure
• Implementations:• C-Array• Single-Linked-List
• Operations:• push: put element on stack (insert as first element)• pop: get first element on stack (remove head)
head
head
push
pop
![Page 40: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/40.jpg)
Associative ContainersDictionaries, Maps, Sets
5 Alpha
key value
3 Beta
7 Gamma
![Page 41: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/41.jpg)
Associative Containers
• Map a key to a value
• Searching for a specific element in unsorted sequential containers takes linear time O(n)
• Getting a specific element from an associative container can be as fast as constant time O(1)
![Page 42: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/42.jpg)
STL Containers
• Associative Containers• map• set• multimap• multiset
• unordered_map (C++11)• unordered_set (C++11)• unordered_multimap (C++11)• unordered_multiset (C++11)
![Page 43: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/43.jpg)
C-Array as Associative Container
• Simplest associative data structure
• maps integer number to data• 0 -> a[0]• 1 -> a[1]• …
• efficient access in O(1)• inefficient storage• limited to positive integer numbers as keys
10
2
7
5
1
4
9
3
int a[10000];
5
0
1
2
3
4
5
6
7
8
![Page 44: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/44.jpg)
Ordered maps
• Maps arbitrary keys (objects, basic types) to arbitrary values (objects, basic types)
• Basic idea: if keys are sortable, we can store nodes in a data structure sorted by its keys. Sorted data structures can be searched more quickly, e.g. with binary search in O(log(n))
• Elements ordered by key
• Worst case lookup time is O(log(n))
![Page 45: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/45.jpg)
std::map#include <iostream>#include <map>#include <string>
using namespace std;
map<string, string> capitals;
// setting value for keycapitals["Austria"] = "Vienna";capitals["France"] = "Paris";capitals["Italy"] = "Rome";
// getting value from keycout << "Capital of Austria: " << capitals["Austria"] << endl;string & capital_of_france = capitals["France"];cout << "Capital of France: " << capitals << endl;
// check if key is setif (capitals.find("Spain") != capitals.end()) {
cout << "Capital of Spain is " << capitals["Spain"] << endl;else {
cout << "Capital of Spain not found!" << endl;}
![Page 46: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/46.jpg)
std::map// iterate over all elementsfor (map<string, string>::iterator it = capitals.begin(); it != capitals.end(); ++it) {
string & key = it->first;string & value = it->second;cout << "The capitol of " << key << " is " << value << endl;
}
// C++11: iterate over all elementsfor (auto it = capitals.begin(); it != capitals.end(); ++it) {
string & key = it->first;string & value = it->second;cout << "The capitol of " << key << " is " << value << endl;
}
// C++11: iterate over all elementsfor (auto & kv : capitals) {
string & key = kv.first;string & value = kv.second;cout << "The capitol of " << key << " is " << value << endl;
}
![Page 47: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/47.jpg)
Unordered maps / Hash maps
• Maps arbitrary keys (objects, basic types) to arbitrary values (objects, basic types)
• On average accessing a hash map through keys takes O(1)• In general unordered structure - you can’t get out objects in
the same order you inserted them.
• a number, called a hash code, is generated using a hash function based on key in O(1)
• Each hash code can be mapped to a location called a bin
• A bin stores nodes with keys which map to the same hash code
• Lookup therefore consists of:• Determining the hash code of the key O(1)• Selecting the correct node inside the bin is in the worst case O(n)
On average lookup times are O(1). But this is only true if there are only few hash collisions.
Hash maps require a good hashing function, which reduces the amount of hash collisions.
Value
hash code: 0x13456
“Somestring”
key: “Somestring”
Value“Otherstring”
Bin
![Page 48: Optimization and Maintainability - SAIFR · Optimisation and Maintainability, David Grellscheid 2015-04-16 Donald Knuth, December 1974: Programmers waste enormous amounts of time](https://reader030.vdocuments.site/reader030/viewer/2022040612/5f028c107e708231d404ce14/html5/thumbnails/48.jpg)
Optimisation and Maintainability, David Grellscheid 2015-04-16
Reusability is an efficiency!
If the student after you has to start from 0, nothing gained