Performance - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Performance

Description:

'Big Oh' notation O(f(n)) Efficiency of performance. expressed as. an approximate function of ... Performance O(n) BUT Different measure // binary search ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 26
Provided by: davidg89
Category:
Tags: big | performance

less

Transcript and Presenter's Notes

Title: Performance


1
Performance O(f(n))
  • What is the problem?

2
Quality software
  • Correctness
  • Performance Efficiency
  • Space how much memory must be allocated?
  • Time how much time elapses during execution?

3
Measuring 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

4
Measuring performance
  • Big Oh notation O(f(n))
  • Efficiency of performance
  • expressed as
  • an approximate function of
  • the number of data elements processed

5
Three 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
6
Three 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

8
Three 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
9
Three 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)
10
Approximate 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
11
Approximate 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)
12
Approximate 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)

13
BUT 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)

14
O(log n) performance
15
O(log n) performance
  • examples
  • binary search in sorted arrays
  • binary search tree operations
  • retrieve, update, insert, delete
  • B-trees
  • heaps
  • insert, remove

16
Sorting 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)

17
Analyzing 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)

18
Comparing 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
19
Combinatorial 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!

20
Combinatorial 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
21
Combinatorial problems
  • only known solutions
  • have n levels of nesting
  • for n data values
  • performance is exponential O(en)
  • includes n!, 2n, 10n,

22
Comparing 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
23
NP 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.

24
P 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)

25
P and NP problems
NP-hard
NP
P
NP-complete
Write a Comment
User Comments (0)
About PowerShow.com