A Laboratory Course in C++Data Structures phần 10 potx

43 412 0
A Laboratory Course in C++Data Structures phần 10 potx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

370 | Laboratory B Test Plan for the heapSort Operation Test Case Array Expected Result Checked Heap ADT | 371 Laboratory B: In-lab Exercise Name Date _ Section _ Examining the tree form of a heap rotated 90 degrees counterclockwise from its conventional orientation can be awkward Because a heap is a complete tree, an unambiguous representation in tree form can be generated by outputting the heap level by level, with each level output on a separate line void writeLevels () const Requirements: None Results: Outputs the data items in a heap in level order, one level per line Only outputs each data item’s priority If the heap is empty, then outputs “Empty heap” The tree shown on the first page of this laboratory, for example, yields the following output 93 82 64 27 75 39 18 Step 1: Implement this operation and add it to the file heap.cpp A prototype for this operation is included in the declaration of the Heap class in the file heap.h Step 2: Activate the 'W' (write levels) command in the test program in the file testb.cpp by removing the comment delimiter (and the character 'W') from the lines that begin with "//W" Step 3: Prepare a test plan for this operation that covers heaps of various sizes, including empty and single-data item heaps A test plan form follows Step 4: Execute your test plan If you discover mistakes in your implementation of the writeLevels operation, correct them and execute your test plan again 372 | Laboratory B Test Plan for the writeLevels Operation Test Case Commands Expected Result Checked Heap ADT | 373 Laboratory B: Postlab Exercise Name Date _ Section _ You can use a heap—or a priority queue (In-lab Exercise 1)—to implement both a first-in, first-out (FIFO) queue and a stack The trick is to use the order in which data items arrive as the basis for determining the data items’ priority values Part A How would you assign priority values to data items to produce a FIFO queue? 374 | Laboratory B Part B How would you assign priority values to data items to produce a stack? Heap ADT | 375 Laboratory B: Postlab Exercise Name Date _ Section _ Part A Given a heap containing 10 data items with distinct priorities, where in the heap can the data item with the next-to-highest priority be located? Give examples to illustrate your answer 376 | Laboratory B Part B Given the same heap as in Part A, where in the heap can the data item with the lowest priority be located? Give examples to illustrate your answer Performance Evaluation Implement a Timer class that you can use to measure the length time between two events—when a function starts and when it finishes, for instance Compare the performance of a set of searching routines Compare the performance of a set of sorting routines Compare the performance of your array and linked list implementations of the Stack ADT Objectives In this laboratory you will: 378 | Laboratory C Overview A routine’s performance can be judged in many ways and on many levels In other laboratories, you describe performance using order-of-magnitude estimates of a routine’s execution time You develop these estimates by analyzing how the routine performs its task, paying particular attention to how it uses iteration and recursion You then express the routine’s projected execution time as a function of the number of data items (N) that it manipulates as it performs its task The results are estimates of the form O(N), O(LogN), and so on These order-of-magnitude estimates allow you to group routines based on their projected performance under different conditions (best case, worst case, and so forth) As important as these order-of-magnitude estimates are, they are by their very nature only estimates They not take into account factors specific to a particular environment, such as how a routine is implemented, the type of computer system on which it is being run, and the kind of data being processed If you are to accurately determine how well or poorly a given routine will perform in a particular environment, you need to evaluate the routine in that environment In this laboratory, you measure the performance of a variety of routines You begin by developing a set of tools that allow you to measure execution time Then you use these tools to measure the execution times of the routines You can determine a routine’s execution time in a number of ways The timings performed in this laboratory will be generated using the approach summarized below Get the current system time (call this startTime) Execute the routine Get the current system time (call this stopTime) The routine’s execution time = startTime Ϫ stopTime If the routine executes very rapidly, then the difference between startTime and stopTime may be too small for your computer system to measure Should this be the case, you need to execute the routine several times and divide the length of the resulting time interval by the number of repetitions, as follows: Get the current system time (call this startTime) Execute the routine m times Get the current system time (call this stopTime) The routine’s execution time = ( startTime Ϫ stopTime ) / m To use this approach, you must have some method for getting and storing the “current system time” How the current system time is defined and how it is accessed varies from system to system Two common methods are outlined below Method Use a function call to get the amount of processor time that your program (or process) has used Typically, the processor time is measured in clock ticks or fractions of a second Store this information in a variable of the following type: typedef long SystemTime; Performance Evaluation You can use this method on most systems You must use it on multiuser or multiprocess systems, where the routine you are timing is not the only program running Method Use a function call to get the current time of day Store this information in a variable of the following type: struct SystemTime { int hour, minute, second, fraction; }; // // // // Hour 0-23 Minute 0-59 Second 0-59 Fraction of a second The range of values for the fraction field depends on the resolution of the system clock Common ranges are 0–99 (hundredths of a second) and 0–999 (thousandths of a second) This method is effective only on single-user/single-process systems where the routine you are timing is the only program running In addition to acquiring and storing a point in time, you also need a convenient mechanism for measuring time intervals The Timer ADT described below uses the familiar stopwatch metaphor to describe the timing process Start the timer Stop the timer Read the elapsed time Timer ADT Data Items A pair of times that denote the beginning and end of a time interval Structure None Operations void start () Requirements None Results Marks the beginning of a time interval (starts the timer) | 379 398 | Appendix To catch any error that occurs within the previous try block, you can use a generic catch block like the following catch ( ) // The ‘ ’ catches all exception that occur { cout

Ngày đăng: 09/08/2014, 12:22

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan