Title: Performance
1Performance O(f(n))
2Quality software
- Correctness
- Performance Efficiency
- Space how much memory must be allocated?
- Time how much time elapses during execution?
3Measuring performance
- Testing
- Analysis of algorithms
- How much space / time is required for an
algorithm? - How does one algorithm compare to another?
- How do requirements change depending on input?
- ? Need a scale for describing performance
4Measuring performance
- Big Oh notation O(f(n))
- Efficiency of performance
- expressed as
- an approximate function of
- the number of data elements processed
5Three simple methods
space
n 6 Memory space
- public void s0(int n)
-
- int val n
public void s1(int n) int val new
intn
public void s2(int n) int val new
intnn
6Three simple methods
- public void s0(int n)
-
- int val n
public void s1(int n) int val new
intn
public void s2(int n) int val new
intnn
7- We will mainly consider
- TIME efficiency
8Three simple methods
time
n 6 of executions
- public void m0(int n)
-
- System.out.println(n)
1
public void m1(int n) for(int i 0 iltn i)
System.out.println(n)
6
public void m2(int n) for(int i 0 iltn i)
for(int j 0 jltn j) System.out.println(n)
36
9Three simple methods
- public void m0(int n)
-
- System.out.println(n)
-
O(1)
public void m1(int n) for(int i 0 iltn
i) System.out.println(n)
O(n)
public void m2(int n) for(int i 0 iltn
i) for(int j 0 iltn i)
System.out.println(n)
O(n2)
10Approximate measure
public void m2(int n) for(int i 0 iltn
i) for(int j 0 iltn i)
System.out.println(n)
public void m2a(int n) for(int i 0 iltn
i) for(int j i iltn i)
System.out.println(n)
Better performance? NO
11Approximate measure
1 2 6 10 100
exec 1 4 36 100 10000
exec 1 3 21 55 5050
n
n2 O(n2)
n2n 2 O(n2)
public void m2(int n) for(int i 0 iltn
i) for(int j 0 iltn i)
System.out.println(n)
public void m2a(int n) for(int i 0 iltn
i) for(int j i iltn i)
System.out.println(n)
12Approximate measure
- // linear search (unsorted array)
- public int search(double n, double arr)
-
- int i 0
- while (i lt arr.length arri ! n)
- i
- return i
-
- From 1 to n iterations
- Failed search n
- Average for successful search n/2
- ? Performance O(n)
13BUT Different measure
- // binary search (sorted array)
- public int search(double n, double arr)
- int i, min 0, max arr.length-1
- while (min lt max)
- i (min max ) / 2
- if (arri n) return i
- if (arri lt n) min i1 else max i-1
-
- return arr.length
-
- From 1 to log n iterations
- Failed search log n
- Average for successful search log n - 1
- ? Performance O(log n)
14O(log n) performance
15O(log n) performance
- examples
- binary search in sorted arrays
- binary search tree operations
- retrieve, update, insert, delete
- B-trees
- heaps
- insert, remove
16Sorting algorithms
- two nested repeat operations
- both O(n) ? O(n2)
- bubble, insertion, selection,
- one O(n) and one O(log n) ? O(n log n)
- mergesort, quicksort, heapsort,
- Shellsort ??, O(n (log n)2), O(n1.25)
17Analyzing an algorithm
- rule of thumb
- depth of nested structure
- (or recursion)
- number of factors
- in performance measure
- some algorithms are difficult to analyze (e.g.
hash table processing)
18Comparing polymomial performance
n factors O(1) 0 O(log n) 1 O(n) 1 O(n log n) 2 O(n2) 2 O(n3) 3
2 1 1 2 2 4 8
8 1 3 8 24 64 512
32 1 5 64 320 1024 32768
128 1 7 128 896 16384 2097152
1024 1 8 1024 8192 1048576 1073741824
19Combinatorial problems
- Example list the possible orderings of S
- 1,2,3 123, 132, 213, 231, 312, 321
- S 2 3 4 5 n
- orderings 2 6 24 120 n!
20Combinatorial problems
P Q PQ
T T T
T F F
F T F
F F F
- Example generate
- truth table for PQ
number of propositions 2 3 4 5 n rows in table
4 8 16 32 2n
21Combinatorial problems
- only known solutions
- have n levels of nesting
- for n data values
- performance is exponential O(en)
- includes n!, 2n, 10n,
22Comparing polymomial (P) andexponential (NP)
performance
n factors O(log n) 1 O(n) 1 O(n log n) 2 O(n3) 3 O(en), e.g. 2n n
2 1 2 2 8 4
8 3 8 24 512 256
32 5 64 320 32768 4294967296
128 7 128 896 2097152 3.4028 x 1038
1024 8 1024 8192 1073741824 1.7977 x 10308
Non-deterministic Polynomial
23NP Non-deterministic polynomial
- if a solution is known, it can be verified in
O(nk) time - e.g., SAT
- a solution (T,T,F,T,F,,T,F,T,F,F,F)
- can be tested by substituting in the fitness
function.
24P and NP problems
- P NP?
- (Are there polynomial algorithms
- for combinatorial problems?)
- YES ? there are but we are too stupid to find
them - NO ? we need to find other ways to solve
these problems - (in 50 years of trying, no answer to the
question)
25P and NP problems
NP-hard
NP
P
NP-complete