Data Structures and Other Objects Using C++ (Chapter 1) 学习笔记三

1.2 RUNNING TIME ANALYSIS

Time analysisconsists of reasoning about an algorithm's speed. Does the algorithm work fast enough for my needs? How much longer does the method take when the input gets larger? Which of several different methods is faster? We'll discuss these issues in this section. An example will help start the discussion.
The Stair--Counting Problem(楼梯)
    Suppose that you and your friend Judy are standing at the top of the Eiffel Tower.How many steps there are to the bottom?
We will look at three different methods that you could use and analyze the time requirements of each.
     Method 1: Walk down and keep a tally(计数器). Each time you take a step down, you make a mark on the sheet of paper. When you reach the bottom, you run back up, show Judy the piece of paper.
     Method 2: Walk down, but let Judy keep the tally. this time you stop after one step, lay your hat on the step, and run back to Judy.
     Method 3: Jervis to the rescue. you spot your friend Jervis by the staircase, holding the sign drawn here: 2689 steps. so you take the paper and pen from Judy, write the number 2689, and hand the paper back to her.
     Instead of measuring the actual elapsed time during each method, we countcertain operations that occur while carrying out the methods. In this example we will count just two kinds of operations:
     1. Each time you walk up or down one step that is one operation.
     2. Each time you or Judy marks a symbol on the paper, that is one operation.
     Of course, each of these operations takes a certain amount of time, and making a mark may take a different amount of time than taking a step. But this does not concern us because we won't measure the actual time taken by the operations.
     Instead, we ask: How many operations are needed for each of the three methods?
     In the first method    you take 2689 steps down, another 2689 steps up, and you also make 2689 marks on the paper, for a total of 3 * 2689 operations----8067 total operations.
     In the second method  also 2689 marks made on Judy's paper, but the total number of operations is considerably more. You start by going down one step and back up one step. Then down two and up two. Then down three and up three, and so forth. the total number of operations taken is:
     Downward steps   =  3,616,705 (which is 1 + 2 + …+ 2689)
     Upward steps       =   3,616,705
     Marks made         =   2689
     Total operations    =   Downward steps + Upward steps + Marks made
                                =    7,236,099
    In the third method  Only four marks are made on the paper(that is, we're counting one "mark" for each digit of 2689), there is no going up and down stairs.
The number of operations used by each of the methods is summarized here:
    Method 1          8067 operations
    Method 2   7,236,099 operations
    Method 3                4 operations

Typical operations for program time analysis

Doing a time analysis for a program is similar to the analysis of the stair-counting methods.
      For a time analysis of a program, we do not usually measure the actual time(实际的时间)taken to run the program because the number of seconds can depend on too many extraneous(额外的) factors----such as the speed of the processor, and whether the processor is busy with other tasks. Instead, the analysis counts the number of operations required.
Dependence on input size
     For most programs, the number of operations depends on the program's input.
     When a method's time depends on the size of the input, then the time can be given as an expression, where part of the expression is the input's size. The time expressions for our three methods are given here:
      Method 1     3n
      Method 2     n + 2 (1 + 2 + … + n)
                         =  n + n(n + 1)
                         =  n + n^2 + n
                         =  n^2 + 2n
      Method 3     The number of digits in the number n
The expressions on the right give the number of operations performed by each method when the stairway has n steps.
Simplification of the Method 3 time analysis
      The number of operations for Method 3 is just the number of digits in the integer n when written in the usual way. The usual way of writing numbers is calledbase 10 notation. As it turns out, the number of digits in a number n, when written in base 10 notation, is approximately equal to another mathematical quantity known as the base 10 logarithm of n. is written log n
      Then the table of the number of operations for each method can now be expressed more concisely, as shown here:      
      Method 1     3n
      Method 2     n + 2 (1 + 2 + … + n)
                         =  n + n(n + 1)
                         =  n + n^2 + n
                         =  n^2 + 2n
      Method 3     The number of digits in the number n. is log n.
Big-O Notation
     Often it is enough to know in a rough manner how the number of operations is affected by the input size. We can express this kind of information in a format called big-O notation.
     We use the notion of "the largest term in a formula." this is the term with the largest exponent(指数) on n, or the term that grows the fastest as n itself becomes larger.
      Quadratic time.  If the largest term in a formula is no more than a constant times n^2, then the algorithm is said to be "big-O of n^2", written as O(n^2), and the algorithm is called quadratic. 
          Linear time. If the largest term in a formula is a constant times n, then the algorithm is said to be "big-O of n", written as O(n), and the algorithm is called linear.
       Logarithmic time. If the largest term in a formula is a constant times a logarithm of n, hen the algorithm is "big-O of the logarithm of n", written O(log n). and the algorithm is called logarithmic.      
       Using big-O notation, we can express the time requirements of our three stair-counting methods as follows:
      Method 1     O(n)
      Method 2     O(n^2)
      Method 3    O(log n).

Order of an algorithm(算法的阶)

      When a time analysis is expressed with big-O, the result is called the orderof the algorithm.
      We want to reinforce one important point:Multiplicative constants are ignored in the big-O notation. For example, both 2n and 42n are linear formulas, so both are expressed as O(n), ignoring the multiplicative constants 2 and 42. As you can see, this means that a big-O analysis loses some information about relative times. Nevertheless, a big-O analysis does provide some useful information for comparing algorithms.
       The order of an algorithm generally is more important than the speed of the processor.

Time Analysis of C++ Functions

void guess_game (int n)
// precondition: n > 0
// postcondition: the user has been asked to think of a number between 1 and n. the function asks a  series of questions until the number is found.
// library facilities used: cassert, iostream
{
    int guess;
    char answer;

    assert (n >= 1);

    cout << "Think of a whole number from 1 to " << n << "." << endl;
    answer = 'N';
    for (guess = n; (guess > 0) && (answer != 'Y') && (answer != 'y'); --guess)
    {
        cout << "Is your number " << guess << "?" << endl;
        cout << "Please answer Y or N, and press return: ";
        cin >> answer;
    }
    if ( (answer == 'Y') || (answer == 'y') )
         cout << "I knew it all along." << endl;
    else
         cout << "I think you are cheating!" << endl;
} 
     For the time analysis. the first step of the time analysis is to decide precisely what we will count as a single operation.
     For C++ functions, a good choice is to count the total number of C++ operations (such as an assignment, the < operation, or the << operation) plus the number of function calls.
     Let's analyze the guess_game function for the case where the parameter is a positive integer n, and the user thinking of the number 1. How many operations does the function carry out in all ? Our analysis has three parts:
     1. Prior to the for-loop, there are seven operations. Thus, before the loop body occurs, there are eight operations.
     2. Execute the body of the loop, because our user is thinking of the number 1, we execute this body n times. let's just say that each execution of the loop body requires k operations. So the total operations is kn.
     3. After the loop finishes, there are five more operations.
     The total number of operations is now kn + 12. Regardless of how big k is, this formula is linear time. So in this case where the user thinks of the number 1, the guess_game function takes linear time.
     In fact, we summarize here:
     A loop that does a fixed amount of operations n times requires O(n) time.
Worst-Case
Average-Case
Best-Case Analysis



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值