Estimating the Efficiency - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Estimating the Efficiency

Description:

Random Number Generator (3.4.8) rand() function. srand() function. 3 ... Random Number Generator. 16. rand() Method is in the header file. stdlib.h or time.h ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 20
Provided by: williamcol
Category:

less

Transcript and Presenter's Notes

Title: Estimating the Efficiency


1
Chapter 3
  • Estimating the Efficiency
  • and
  • Randomness

2
Outline
  • Software-development Life Cycle
  • Skip section 3.2 3.4.2
  • Big-O notation (3.4.3 3.4.5)
  • Growth Rates
  • clock() function
  • time() function
  • Random Number Generator (3.4.8)
  • rand() function
  • srand() function

3
Software-development Life Cycle
  • Problem Analysis
  • Determine what is to be done
  • System Tests
  • Sample input values are created and the
    corresponding output produced by hand
  • Program Design
  • What classes will be needed to solve the problem
    and how those classes are related
  • Program Implementation
  • Determine the correctness and efficiency of each
    of the classs methods

4
Mathematical Definition f(n) O(g(n))
  • We say that f(n)O(g(n)) if there are positive
    constants C and K such that f(n) ? Cg(n) when n
    ? K.
  • Here, we are concerned with n being positive real
    numbers (usually positive integers)
  • Examples
  • f(n) n O(n) with g(n) n, C 1, K 1
  • f(n) 10n O(n) with g(n)n, C10, K 1
  • Note that f(n) 10n O(n2) with g(n) n2, C
    1, K 10
  • Also, f(n) 10n O(n2) with g(n) n2, C 2, K
    5
  • Hence, g(n), C and K are not unique

5
Big-O notation
  • g(n) is an upper bound for f(n)
  • We say that f(n) is no larger than g(n)
  • We say f(n) is of order of g(n)
  • We say f(n) is Big-O of g(n), i.e., f(n)
    O(g(n))
  • We say the growth rate of f(n) is less than or
    equal to the growth rate of g(n)

6
Graphic Representation of f(n) O(g(n))
K
7
Any Algorithm that is O(n2) is also O(n3)
K
K
8
Read Section 3.4.4 and 3.4.5
  • log (n) The number of times to divide n by 2
    until n lt 1 is log2n
  • worstTime(n) Analysis
  • Examples 1 through 5 Text pages 77 81

9
Common Functions
10
Growth Rates
worstTime(n)
O(2n)
O(n2)
O(n log n)
O(n)
O(log n)
O(1)
n
11
Example of Run Times
Assume 1 operation per microsecond (0.000001)
12
Examples
Give the asymptotic notation (O-notation) of the
following functions and rearrange them in
ascending order by rate of growth. (Hint
)
13
clock() function
  • clock() This function returns the number of
    clock ticks (the CPU time taken) the program has
    taken. To convert to the number of seconds,
    divide by CLOCKS_PER_SEC, a constant that is
    defined in time.h
  • To determine the amount of time a section of code
    takes to run,
  • include lttime.hgt//include ltstdlib.hgt
    //alternativefloat timertimer clock()
    //clock() returns the time in terms of ticks
  • // Perform tasktimer clock() - timer
    //Gives the elapsed time in tickstimer /
    CLOCKS_PER_SEC //Converts ticks to
    secondstimer / 60 //Converts
    seconds to minutes

14
time() function
  • To determine the amount of time (in seconds) a
    section of code takes to run, use the following
    code
  • includelttime.hgt// include ltstdlib.hgt //
    alternative long timer time(NULL)
  • //Perform tasktimer
    time(NULL) - timer //Gives the elapsed time in
    seconds

15
Random Number Generator
16
rand()
  • Method is in the header file
  • stdlib.h or time.h
  • The header file has a compiler-dependent constant
    RAND_MAX
  • The following program will generate a random
    number between 0 and RAND_MAX (0x7FFFFFFF,
    2,147,483,647)
  • include ltiostreamgt
  • include ltstdlib.hgt
  • using namespace std
  • int main ( )
  • cout ltlt rand()
  • return 0

17
rand()
  • To find a random number between 0 and some int
    high, use the remainder portion of divide.
  • The following program will generate a random
    number between 1 and 100
  • include ltiostreamgt
  • include ltstdlib.hgt
  • using namespace std
  • int main ( )
  • int high 100
  • cout ltlt rand() high 1 ltlt endl
  • return 0

18
rand()
  • Unfortunately, every execution of the program
    results in the same random number.
  • The function depends on an initial value, called
    a seed.
  • There is a function which allows the initial
    value to be changed.
  • srand(seed)
  • where seed is an integer.
  • The following program will generate a random
    number between 1 and 100 depending on the
    inputted integer.
  • include ltiostreamgt
  • include ltstdlib.hgt
  • using namespace std
  • int main ( )
  • int high100
  • int seed
  • cin gtgt seed
  • srand(seed)
  • cout ltlt rand() high 1 ltlt endl
  • return 0

19
time()
  • include ltiostreamgt
  • include ltstdlib.hgt
  • // include lttime.hgt
  • // alternative
  • using namespace std
  • int main ( )
  • int high 100
  • long seed time(NULL)
  • srand(seed)
  • cout ltlt rand() high 1
  • return 0
  • A better method would be to use something that is
    constantly changing, time
  • could use the header file time.h instead of
    stdlib.h
  • Contains a function
  • time()
  • returns a long value representing the time
    elapsed in seconds since 01/01/1970.
  • The following program will generate a different
    random number between 1 and 100 each time it is
    run (unless you run it twice in a second).

Text P85 (lab 5) generates a sequence of
pseudo-random numbers
Write a Comment
User Comments (0)
About PowerShow.com