Title: Performance Analysis and Measurement
1Chapter 12
- Performance Analysis and Measurement
2- Time Complexity
- Performance Analysis
- Step Counts
- Asymptotic Notation
- Performance Measurement
3- performance evaluation can be loosely divided
into two major phases - a priori estimates, or performance analysis
- a posteriori testing or performance measurement
- The time complexity of a program is the amount of
computer time it needs to run to completion
4 The Running time of a Program depends on
The input to the program. The quality of
code generated by the compiler used to
create the object code. The nature
and speed of the instructions on the machine
used to execute the program The time
complexity of the algorithm underlying the
program.
5Step Counts
- A program step is loosely defined as a
syntactically or schematically meaningful segment
of a program that has an execution time that is
independent of the instance characteristics. - to determine the number of steps needed by a
program to solve a particular problem instance,
compute the step counts for each statement
6- Example
- float sum (float a, const int n)
- float s0
- for (int i0 iltn i)
- s ai
- return s
7- float sum (float a, const int n)
- float s0
- count //count is global
- for (int i0 iltn i)
- count //for for
- s ai
- count // for assignment
-
- count //for last time of for
- count //for return
- return s
8- if count is zero to start with, then it will 2n3
on termination
9- Example (matrix addition)
- void add (matrix a, matrix b, matrix c,
- int m, int n)
- for (int i0 iltm i)
- for (int j0 jltn j)
- cijaij bij
10- void add (matrix a, matrix b, matrix c,
- int m, int n)
- for (int i0 iltm i)
- count // for for i
- for (int j0 jltn j)
- count //for for j
- cijaij bij
- count //for assignment
-
-
11- count //for last time of for j
-
- count //for last time of for i
-
- if count is zero to begin with, it will be
2mn2m1 when program terminates
12Asymptotic Notation
DEFINITION Asymptotic is the study of functions
of a parameter n, as n becomes larger and larger
without bound
130 - notation
asymptotic upper bound f (n) O (g (n))
iff there exists a positive constant C, and
n0 such that f(n) lt c g (n) for all
n, n gtn0
14 - notation
asymptotic lower bound for a given function
g (n) f (n) (g (n)) iff there
exists a positive constant C, and n0 such that
f(n) gt c g (n) for all n, n gt n0 .
15 - notation
asymptotically tight bound f (n)
(g (n)) iff there exists positive constants
C1 C2 , and n0 such that C1 g(n)
lt f(n) lt C2 g (n) for all n, n gt n0
16Graphic example of , O, notation
17(No Transcript)
18(No Transcript)
19Examples
3n 2 O (n) is 3n 2 lt 4n
for all n gt 2. 1000 n2 100n - 6 O (n2 )
as 1000 n2 100n - 6 lt 1001
for all n gt 100.
20 Examples
3n 2 (n) is 3n 2 gt 3n
for all n gt 1. 100 n 6 (n) is
100n 6 gt 100n for n gt 1.
21 Examples
3n 2 (n) is 3n 2 gt
3n for all n gt2 3n 2 lt4n for
all n gtn0 . so, C1 3, C2 4, n0
2
22Theorem
if f(n)
then f(n) O( )
23 The rule of sums suppose (n) and
(n) are the running times of two program
fragments and and (n)
O (f(n)) and (n) O(g(n)) then
(n) (n) O (max (f(n),
g(n))
24Example
25The rule for Products
Assume
26Calculating therunning time of a Program
void Bubble (int A , int n) int i,
j, temp 1. for (i 0 i lt n - 1 i) 2.
for (j n - 1 j gt i 1 -- j) 3. if
(Aj - 1 gt Aj) 4. temp
Aj-1 5. Aj - 1 Aj 6.
Aj temp
27 Each assignment statement takes some
constant time, independent of the input
size. Lines (4), (5), and (6) each takes
O(1) time. Combined Running time
O (max (1, 1, 1)) O(1) Line (3) takes
O(1) time.
28 Consider the worst case Running time
Therefore, statements (3) - (6) takes O(1)
time Lines (2) - (6) each loop
iteration takes O(1) time The of loop
iteration is n - i - 1 Therefore,
O((n-i) 1) O(n-i) line (1) is
executed n - 1 times
29 Therefore the total Running time of the
program is
30Common orders
O(1) means computing time that is bounded
by a constant (not dependent on n). O
(n) means that the time is directly
proportional to n, and is called linear
time. O( n2 ) is called quadratic time,
O( n3 ) cubic, O( 2n ) exponential.
These five orders, along with logarithmic
time O(log n) and O(nlog n), are the ones
most commonly used in analyzing algorithms.
31- to get a feel for how various functions grow with
n - logn n nlogn n2 n3 2n
- 0 1 0 1 1 2
- 1 2 2 4 8 4
- 2 4 8 16 64 256
- 3 8 24 64 512 256
- 4 16 64 256 4096 65,536
- 5 32 160 1024 32,768 4,294,967,296
32- Consider a 1-billion steps-per-second computer,
then the time for f(n) , when n1000 is as
follows - n10 n100 n1000
- f(n) n .01ms .10ms 1.00 ms
- f(n)logn .03ms .66ms 9.96ms
- f(n)n2 .1ms 10ms 1ms
- f(n)n3 1ms 1ms 1sec
- f(n) n4 10ms 100ms 16.67min
- f(n)n10 10sec 3171 yr 3.171013 yr
- f(n) 2n 1ms 41013 yr 3210283 yr
33Performance Measurement
- Performance measurement is concerned with
obtaining the actual time requirements of a
program. - This quantity depends on the particular compiler
and options used as well as on the specific
computer on which the program is run