Measuring Running Time - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Measuring Running Time

Description:

We will assume that running time for simple statements that are independent of ... First we determine running time of methods that do not call any other methods. ... – PowerPoint PPT presentation

Number of Views:93
Avg rating:3.0/5.0
Slides: 10
Provided by: IRE62
Category:

less

Transcript and Presenter's Notes

Title: Measuring Running Time


1
Measuring Running Time
  • Irena Pevac
  • CS501

2
Running Time of Simple Statements
  • We will assume that running time for simple
    statements that are independent of the size of
    input is O(1).
  • Arithmetic operations (,-,,/,)
  • Logical operations (, , !)
  • Comparison operations (,)
  • Simple assignment statements
  • Method invocations

3
Running Time of Simple Loops
  • In loops we determine the number of times the
    loop body will be performed. If that is O(n)
    where n is the size of the input, and if loop
    body contains only simple statements than running
    time of the loop is O(n). If loop body works in
    O(n) than loop works in O(n2).

4
Running Time of Loops Without Method Invocations
  • Page 121 Fig 3.13
  • O(f(n) g(n))
  • f(n) is runnig time of the loop body
  • g(n) specifies the number of iterations

5
Running Time of Selection Statements
  • if ()
  • else
  • If condition does not invoke any methods its
    running time is O(1), so we can neglect it.
  • Let f(n) be running time of true-path statements
    and let g(n) be running time of false-path
    statements. We can bound running time of if
    statement as O(max(f(n),g(n)).
  • Page 122 Fig 3.14

6
Running Time of Blocks
  • One block consisting of several O(1) statements
    has running time O(1).
  • If at least one statement requires f(n) steps
  • Running time O(f(n)).
  • If sequential list of statements has running
    times f1(n), f2(n),,fk(n), respectively, then
    running time of entire list is O(max(f1(n),
    f2(n),,fk(n) ))
  • If two consequtive blocks have running times f(n)
    and g(n) then together they have running time
    O(max(f(n), g(n)).
  • Page 123 Fig 3.15

7
Analyzing Recursive Methods
  • Assign TF(n) to be running time of a method with
    size n. TF(n) is unknown function of n.
  • Establish inductive definition called recurrence
    relation for TF(n) that relates to functions
    TG(k) for other functions G and their associated
    argument size s k. For direct recursion F and G
    are the same.
  • When argument size is sufficiently small that no
    recursive call are made by F, this is base case.
  • For larger argument sizes, one or more recursive
    calls are made, but with smaller argument sizes.

8
Analyzing Running Time of Code with Method Calls
  • Program fragment with method calls
  • First we determine running time of methods that
    do not call any other methods.
  • Next we determine running time of methods that
    only call methods whose running time we
    determined. And so on until we evaluate running
    time of all methods.
  • If a method A calls method B, we must relate the
    size measure for the arguments of B to the
    measure of size that is used for A.

9
  • Assume that method A has a tight upper bound
    O(h(n)) where n is a measure of the size of the
    arguments of A.
  • When a call to A is made within a simple
    statement we add O(h(n)) to the cost of that
    statement.
  • When a call to A is made in a condition of a loop
    we add h(n) to the bound on the time for each
    iteration.
  • When a call to A is made in the initialization of
    a for-loop we add h(n) to the cost of the loop.
  • When a call to A is made in a condition of an if
    statement we add h(n) to the bound on the time
    for each statement (true-paths and false-path).
Write a Comment
User Comments (0)
About PowerShow.com