Performance Analysis


Document last modified: 

Overview

Algorithms can be analyzed by performance several measures: run time and memory space being the most common. 


Example - Constant Time Operation

  • The number of instructions executed by a constant time operation is not dependent on the size of the problem on which the operation is working.
  • Swap is an example of a constant time operation. O(1)
Swap (x, y)
-- alters x, y
-- post: x = old(y)  and  y = old(x)
    Executions
1 temp ¬ x 1
2 x ¬ y 1
3 y ¬ temp 1
  • This operation correctly performs its function by executing exactly 3 statements, each constant time.
  • The size of the problem N would include the size of both integers that are being swapped.
  • The problem size of the Swap is for any N.
  • f(N) = 3 * N0 - since N raised to the power zero is 1, f(N) = 3
  • This is known as a constant function, where the slope is 0.
  • In Big-O notation, this operation is referred to as an O(1) operation or a constant time operation.

Example - Linear Time Operation

  • The number of instructions executed by a linear time operation is linearly dependent on the size of the problem on which the operation is working.
  • The Display operation for a Queue is an example of a linear time operation, O(n)

< "red" "C343" "Fall" >

Display (q)
-- preserves q
-- post: contents of q are displayed
              Executions
s1 i ¬ 1   1
s2 while i ≤ length[q]   N+1
s3    x ¬ Dequeue (q)   N
s4    print x   N
s5    Enqueue (q, x)   N
s6    i ¬ i + 1   N

The size of the problem N depends on the number of items in the queue.

N = length[q]

The first statement (s1) gets executed exactly 1 time no matter how many items are in the queue.

  • s1 contributes 1 * N0 instructions to the total.
  • Statement s2 gets executed N + 1 times.    Question: Why?
  • Statements s3, s4, s5, and s6, get executed N times
  • So these statements contribute 4 * N1 instructions to the total

f(N) = (1 * N0) +  (N1 + 1) + (4 * N1)
       = 5 * N1 + 1 * N0
        
= 5 * N1 + 1
        = 5N + 1

This is is a linear function where the slope is 5 and the y-intercept is 1.

In Big-O notation, this operation is referred to as an O(N) operation or a linear time operation.

5N + 1 is O(N)


Polynomial Time Operation

  • The number of instructions executed by a polynomial time operation is dependent on the size of the problem on which the operation is working.
  • The Insertion-Sort is an example of a polynomial time operation, O(n2)

The size of the problem N depends on the number of items in the array.

There are two loops, one nested within the other.

The best-case occurs when the array is sorted; the while is not iterated.

The worst-case occurs when the array is reverse sorted, O(n2)